String类型提供了正则表达式替换方法,如:replaceFirst,replaceAll,它们其实是对应调用
java.util.regex.Matcher的同名方法
如果你仔细看它们的方法说明就会看到
Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string;
请注意,替换字符串中的反斜杠(\)和美元符号($)可能会导致结果与将其视为文字替换字符串时的结果不同;
Illegal group reference
也就是说反斜杠(\)和美元符号($)在替换字符中有特别含义。比如$1在替换字符串中代表表达式匹配的捕获组1(capture group),在替换过程中如果遇到$符号,后面就必须是个0-9数字,否则就会抛出异常IllegalArgumentException:Illegal group reference
如下的代码中替换数据中为${hello}就会复现此异常:
import static org.junit.Assert.assertTrue;
import java.util.regex.Matcher;
import org.junit.Test;
public class RegexTest {
public static final String NEW_LINE = System.getProperty("line.separator");
private static final String commentBody = "/**" + NEW_LINE + " */" + NEW_LINE;
@Test
public void testReplaceFirst() {
try {
String comment = "$(hello)";
/** 在第一个换行符后面增加${hello} */
String cmt = commentBody.replaceFirst(NEW_LINE, "$0" + comment);
System.out.println(cmt);
} catch (Exception e) {
e.printStackTrace();
assertTrue(false);
}
}
}
Matcher.quoteReplacement
Matcher中提供了quoteReplacement静态方法用于将替换字符串中的\和$符号转义
要避免上面的问题,就可以如下在替换前将${hello}内容调用quoteReplacement方法转义处理就不会再抛出异常
import static org.junit.Assert.assertTrue;
import java.util.regex.Matcher;
import org.junit.Test;
public class RegexTest {
public static final String NEW_LINE = System.getProperty("line.separator");
private static final String commentBody = "/**" + NEW_LINE + " */" + NEW_LINE;
@Test
public void testReplaceFirst() {
try {
String comment = "$(hello)";
System.out.println(Matcher.quoteReplacement(comment));
/** 在第一个换行符后面增加${hello} */
String cmt = commentBody.replaceFirst(NEW_LINE, "$0" + Matcher.quoteReplacement(comment));
System.out.println(cmt);
} catch (Exception e) {
e.printStackTrace();
assertTrue(false);
}
}
}
输出
$(hello)
/**
$(hello) */
文章介绍了Java中String类型的replaceFirst和replaceAll方法在进行正则替换时,如何处理反斜杠()和美元符号($)的特殊含义。当替换字符串包含这些特殊字符时,可能导致意外的结果或抛出异常。为了解决这个问题,文章提到了Matcher类的quoteReplacement方法,该方法可以用来转义这些特殊字符,确保替换过程按照预期进行。
957

被折叠的 条评论
为什么被折叠?



