My code is
String debug = "1$<$2";
Matcher matcher = Pattern.compile("^[1-5]" + "\\(?\\$[^\\$]*\\$\\)?" + "([1-5])$").matcher(debug);
List<String> matches = new ArrayList<String>();
if (matcher.matches()) {
for (var i = 0;i< matcher.groupCount();i++){
matches.add(matcher.group(i));
}
}
System.out.println(matcher.groupCount());
System.out.println(matches);
And the group count is only 1.
The matches is 1$<$2.
But actually the result of matcher.group(1) is 2.
How can I get the right group count?
([1-5])at the end.matcher.group(0)will always be the full match. I.e. the input string. From the javadoc: Group zero denotes the entire pattern, so the expression m.group(0) is equivalent to m.group()