I am a bit stuck in regex. Consider the following code:
String regFlagMulti = "^(\\[([\\w=]*?)\\])*?$";
String flagMulti = "[TestFlag=1000][TestFlagSecond=1000]";
Matcher mFlagMulti = Pattern.compile(regFlagMulti).matcher(flagMulti);
if(mFlagMulti.matches()){
for(int i = 0; i <= mFlagMulti.groupCount(); i++){
System.out.println(mFlagMulti.group(i));
}
}else{
System.out.println("MultiFlag didn't match!");
}
What I want is a regular template that gives me the text inside [] ; each of which is in the group of the resulting Matcher object.
Important: I do not know how many [] expressions are inside the input string!
For the code above, it produces:
[TestFlag=1000][TestFlagSecond=1000]
[TestFlagSecond=1000]
TestFlagSecond=1000
I can not get the regular pattern to work. Any idea?
source
share