在scala中,如何在字符串中查找所有出现的\\ $ [0-9] +并替换它们

时间:2015-12-10 01:02:58

标签: regex scala

以下简单代码失败(我希望结果为"abc$1%s"):

  "\\$[0-9]+".r
    .replaceAllIn(
      "abc$1", {
        mtch =>
          mtch.toString() + "%s"
      })

No group 1
java.lang.IndexOutOfBoundsException: No group 1

我已正确转义美元符号并避免所有前瞻/回顾控制字符,为什么它仍然失败?

1 个答案:

答案 0 :(得分:3)

The group error means you don't have a parenthesis delimited group defined.

Something like "(\\$[0-9]+)" would define your whole regex as a group.