This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
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 Building Microservices this week in the Design 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: 1541
    
    5

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


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
 
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: Doubt regarding Regex "?" quantifier
 
It's not a secret anymore!