java - 下面这句话 : 是什么意思

标签 java regex stringbuffer

来自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/

相关文章:

java - 哪些读卡器支持 Java Card?

java - 防止在 Jsf2 中未经登录访问受限页面

regex - Perl 在另一个字符串中间添加字符串

java - 哈夫曼码解释Java

java - Java中如何将整数转换为字符? (5 --> '5' )

java - StringBuffer 如何与 .append 一起使用

java - 使用 Netty 作为代理后面的客户端?

java - 确定 Java 中 TLS 握手的 Diffie-Hellman "Parameters"长度

javascript - 如何在 Webload PT 工具中使用 javascript 进行关联时使用正则表达式提取器?

javascript - 如何判断复制的内容是来自word文档还是浏览器网页?