It's not a secret anymore!
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 REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Reply locked New topic

Doubt regarding Regex "?" quantifier

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157


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

Joined: Dec 08, 2010
Posts: 1544

Duplicate of

Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
I agree. Here's the link:
subject: Doubt regarding Regex "?" quantifier
It's not a secret anymore!