I want to combine one word inside brackets (including brackets), mine Regexworks below, but it does not return me all groups.
Here is my code:
String text = "This_is_a_[sample]_text[notworking]";
Matcher matcher = Pattern.compile("\\[([a-zA-Z_]+)\\]").matcher(text);
if (matcher.find()) {
for (int i = 0; i <= matcher.groupCount(); i++) {
System.out.println("------------------------------------");
System.out.println("Group " + i + ": " + matcher.group(i));
}
I also tested it in Regex Planet and it seems to work.
He should return 4 groups:
Group 0: [sample]
Group 1: sample
Group 2: [notworking]
Group 3: notworking
But he returns only this:
Group 0: [sample]
Group 1: sample
What's wrong?
source
share