I’m not able to get this to match correctly. It only prints “[help]” but I want it to match the characters inside the brackets.
want:
[help]
help
code:
Pattern p = Pattern.compile("\\[(\\w+)\\]");
Matcher m = p.matcher("[help]");
m.find();
for (int i=0;i<m.groupCount(); i++) {
System.out.println(m.group(i));
}
You need to check for
<=for the groupCount. Like so:From the Matcher Javadoc: