我有下面的常规代码
import java.util.regex.Matcher;
import java.util.regex.Pattern;
String myInput="License_All (12313)"
String myRegex="\\(\\d+\\)"
String ResultString
Pattern regex
Matcher regexMatcher
regex = Pattern.compile(myRegex, Pattern.DOTALL);
regexMatcher = regex.matcher(myInput);
List<String> matchList = new ArrayList<String>();
while (regexMatcher.find()) {
matchList.add(regexMatcher.group());
}
for(int i=0;i<matchList.size();i++)
{
myInput=myInput.replaceAll( matchList[i], '')
println matchList[i]
}
println myInput
它应该删除 (12313)
部分,但它给了我以下输出
(12313)
License_All ()
如何删除所有括号部分(12313)
?
最佳答案
matchList
将包含匹配的部分,即 (12313)
。然后,该值再次被视为 replaceAll
中的正则表达式,因此被解释为组。因此括号不会被删除。
相反,您可以直接在 replaceAll
中使用正则表达式:
myInput.replaceAll(myRegex, "")
关于java - 在groovy中使用正则表达式进行模式匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7580258/