Is it possible to find all groups in a regular expression that match a specific part of a String?
Pattern pattern = Pattern.compile("(green trousers)|(green\\s+t)");
Matcher matcher = pattern.matcher("my beautiful green trousers are red!");
while (matcher.find()) {
for (int i = 1; i <= matcher.groupCount(); i++) {
if (matcher.group(i) != null) {
System.out.println("group " + i + " matched");
}
}
}
This example only returns the first group as matching, but I also interested in the fact that the second group matches too.