VISHAL SAXENA VISHAL SAXENA - 2 months ago 7
Java Question

Please justify the output in Regex Java program

I have came across one Java program in Regex .

Below is the program code :

import java.util.regex.*;

public class Regex_demo01 {

public static void main(String[] args) {
boolean b=true;
Pattern p=Pattern.compile("\\d*");
Matcher m=p.matcher("ab34ef");

while(b=m.find())
{
System.out.println(b);
System.out.println(">"+m.start()+"\t"+m.group()+"<");
}


}

}


Output :

true
>0 <
true
>1 <
true
>2 34<
true
>4 <
true
>5 <
true
>6 <


Doubt : As we all know that The find() method returns true if it gets a match and remembers the start position of the match. If find() returns true, you can call the start() method to get the starting position of the match, and you can call the group() method to get the string that represents the actual bit of source data that was matched.
My question is how come ">6 <" is present is the output when the string indexing is till index 5 ?

Answer

My question is how come >6 < is present is the output when the string indexing is till index 5 ?

That behavior is due to your regex i.e. \\d* which matches 0 or more digits.

As you can see it is showing start position 0 as well when there is no digit at the start.

Similarly 6 is last index +1 because there is an empty match past the last character as well.

You should use \\d+ as your regex.