I try to parse the string /param1=value1/param2=value2, to extract the param/value pairs with this code :
public static void main(String[] foo) {
RegExp regExp = RegExp.compile("(/(\\w+)=(\\w+))*", "g");
MatchResult matchResult = regExp.exec("/param1=value1/param2=value2");
for (int i = 0; i < matchResult.getGroupCount(); i++) {
System.out.println("group" + i + "=" + matchResult.getGroup(i));
}
}
which outputs :
group0=/param1=value1/param2=value2
group1=/param2=value2
group2=param2
group3=value2
I tried with and without the “g” (global) flag.
Why does it capture only param2=value2 and not also param1=value1?
Remove the
asterisk(*) from your regex.