I have the below groovy code
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
It is supposed to remove (12313) part but instead it gives me the below output
(12313)
License_All ()
How do i remove all the bracket portion (12313)?
The
matchListwill contain the matched part, i.e.(12313). This one is then again taken as regular expression withinreplaceAlland therefore interpreted as group. Thus the brackets are not removed.Instead you can use your regex directly in
replaceAll: