public static void main(String args[]) {
Pattern p = Pattern.compile("ab"); // Case 1
Pattern p = Pattern.compile("bab"); // Case 2
Matcher m = p.matcher("abababa");
while(m.find()){
System.out.print(m.start());
}
}
When I used Case 1, then output is 024 as expected. But, when I used Case 2 then output is 1, but I was expected 13. So, anyone explain me, is there any exceptional rule in regex, which causes this output, if not. Then, why I’m getting this output.
Help appreciated !!
Note : Case 1 and Case 2 are independently used.
The match consumes the input, so the next match is found after the end of the previous match:
Position of “bab” matcher’s pointer before each match would be:
|abababaabab|aba