wood burning stoves 2.0*
The moose likes Java in General and the fly likes Doubt regarding Regex Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Reply locked New topic
Author

Doubt regarding Regex "?" quantifier

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Hi

I tried out the following code :



The output is:

0 0
1 2 a
2 2
3 4 a
4 4

a) Why is matcher.start() and matcher.end() returning 4 4 as output? The source string itself has only 3 zero-based index positions("baba").

b) Secondly, when an instance of "a" is found , matcher.end() returns the next index position. Like when it finds "a" at index position 1, it returns 2 for matcher.end(). Why so?


~ Mansukh
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1505
    
    5

Duplicate of http://www.coderanch.com/t/604628/java-programmer-SCJP/certification/Regex-quantifier


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Doubt regarding Regex "?" quantifier