문제

From 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.

I get the first bit, However a little confuse about the part underscored in bold. What do they mean with "to escape literal characters"? what kind of literal characters would you escape in a replacement string?

Thanks in advance.

도움이 되었습니까?

해결책

Well, the $ ;)

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"));
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top