Scala Regex匹配和替换

时间:2015-07-28 15:03:37

标签: regex scala

我想在字符串中找到模式id | bin | AnalytePeak | QC 1 | 1 | 620 | 1 | 2 | 1020 | 1 | 3 | 681 | 1 1 | 4 | 1190 | 1 | 5 | 1200 | --------------------------- 2 | 1 | 1020 | 2 | 2 | 1076 | 2 | 3 | 1190 | 2 | 4 | 1200 | 2 | 5 | 358 | 1 --------------------------- 3 | 1 | 1020 | 3 | 2 | 1076 | 3 | 3 | 1190 | 3 | 4 | 1200 | 3 | 5 | 1358 | ,并从字符串中找到的模式的所有实例分别从开头和结尾删除"""("\"\{.*?\}\"")""""{

例如:}"应替换为"batters": "{{"id":"1001"}}"

有没有可以帮助我的正则表达式功能?

1 个答案:

答案 0 :(得分:1)

Happen to see a detailed explanation about this usage of regular expression in JavaScript: The Definitive Guide 6th Edition 10.2 String Methods for Pattern Matching

Recall that parenthesized subexpressions of a regular expression are numbered from left to right and that the regular expression remembers the text that each subexpression matches. If a $ followed by a digit appears in the replacement string, replace() replaces those two characters with the text that matches the specified subexpression

scala> """"{{"id":"1001"}}"""".replaceAll("""\"\{(.*?)\}\"""", "$1")
res15: String = {"id":"1001"}

The code above should solve you problem.