来自this java API:
Dollar signs may be treated as references to captured subsequences as described above, and backslashes are used to escape literal characters in the replacement string.
我明白了第一点,但是对粗体下划线的部分有点困惑。 “转义文字字符”是什么意思?您会在替换字符串中转义哪种文字字符?
提前致谢。
最佳答案
好吧,$
;)
public static void main(final String... args)
{
final Pattern p = Pattern.compile("one dollar");
final String input = "I want one dollar, please";
// IndexOutOfBoundsException: no group 1
System.out.println(p.matcher(input).replaceFirst("$1"));
// You need to escape the "$"
System.out.println(p.matcher(input).replaceFirst("\\$1"));
}
关于java - 下面这句话 : 是什么意思,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17630763/