我正在尝试用正则表达式将一些代码替换为另一个代码。我正在使用 Java,但我认为它与问题无关
String testString = "sb.Append(\"first string to append(1) \"); sb.Append(\"second string to append(2)\");";
Pattern appendPattern = Pattern.compile("\\s*(\\w+)\\.Append\\((.*)\\);");
Matcher appendMatcher = appendPattern.matcher(testString);
System.out.println(appendMatcher.replaceAll("[$1 appendString: $2];"));
我的预期结果是:
[sb appendString: "first string to append(1) "]; [sb appendString: "second string to append(2)"];
我得到的是:
[sb appendString "first string to append(1) "); sb.Append("second string to append(2)"];
在决定比赛结束位置时,.*
优先于 \\)
。
我哪里错了?
最佳答案
关于java - 用于在测试字符串中重复文本的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23786064/