Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt regarding Regex "?" quantifier

 
Mansukhdeep Thind
Ranch Hand
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I tried out the following piece of code given in SCJP for Java 6:



The output is :

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


I have a small doubt regarding the "?" quantifier which is described as "fetches zero or one instance of the compiled pattern being supplied to the matcher source".

a) Why am I getting the above output? What does "a?" pattern mean? Of course it means find zero or at least 1 occurrence of "a" in source string. So why is matcher.end() returning next index position from found "a" instance? When it finds "a" at position 1, it prints matcher.end() as 2. Why?

Secondly, why is 4 4 there in the output? The index positions are only till 3 right? ("baba") Then why does it print the one that is not there in the source string anyway? Please help me understand what is happening here.
 
Campbell Ritchie
Marshal
Posts: 53094
124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write down all the places in that text where there is an a. Now write down all the places where there are 0 a-s.
Remember that is an exam question, so it is designed to confuse you.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Write down all the places in that text where there is an a. Now write down all the places where there are 0 a-s.
Remember that is an exam question, so it is designed to confuse you.


Ok. So I understood all the returned output except 4 4. The index position 4 isn't even part of the source string "baba". It has only 3 zero based index positions. Then why does it return 4 4?
 
Campbell Ritchie
Marshal
Posts: 53094
124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The a at position 3 is followed by no a-s.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:The a at position 3 is followed by no a-s.


We can say on similar lines that the first "b" is preceded by 0 a-s.
 
Himai Minh
Ranch Hand
Posts: 1396
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind wrote:Hi



The output is :

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


Secondly, why is 4 4 there in the output? The index positions are only till 3 right? ("baba") Then why does it print the one that is not there in the source string anyway? Please help me understand what is happening here.


It is not intuitive. a? may match with a zero length string. So, it always returns the very last zero length substring, which is an empty string at the very end of the input string.
This is a tricky part of the exam. Take a look at one of the "Exam Watch" sessions in KB's book.
 
Sergej Smoljanov
Ranch Hand
Posts: 470
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

result:
0 0
1 2 a
2 2
3 3
4 5 a
5 5
Can anyone explain how matcher.end() work, what it find?
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!