【问题标题】:What do they mean with the following sentence:下面这句话是什么意思:
【发布时间】:2013-07-11 22:21:39
【问题描述】:

来自thisjava API:

美元符号可以被视为对捕获的子序列的引用 如上所述,和反斜杠用于转义文字字符 在替换字符串中。

我得到了第一个,但是对粗体下划线的部分有点困惑。 “转义文字字符”是什么意思?你会在替换字符串中转义什么样的文字字符?

提前致谢。

【问题讨论】:

  • 美元符号。 "\\$"
  • @johnchen902 那他们为什么用复数呢?
  • 不知道,我语法不好。

标签: java regex stringbuffer


【解决方案1】:

嗯,$ ;)

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"));
}

【讨论】:

  • 他们为什么用复数呢? “转义字符”?
  • 因为替换字符串中可以有多个$?老实说,我看不出还有什么其他角色需要转义...
  • 我注意到你很喜欢正则表达式,我建议你这个问题stackoverflow.com/questions/17618812/…随时回答:)
猜你喜欢
  • 2011-10-15
  • 1970-01-01
  • 2013-05-28
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 2010-10-03
  • 2013-08-02
相关资源
最近更新 更多