aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Confusion Zero Length Match Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Confusion Zero Length Match" Watch "Confusion Zero Length Match" New topic
Author

Confusion Zero Length Match

Been Zaidi
Greenhorn

Joined: Mar 21, 2012
Posts: 8

Hi,
I was reading greedy quantifier in K&B. I came across zero-length match which are pretty tricky. Here is a slight modification
to the exam and that's the input given from the console



From the console here is the input given


In my opinion, the instruction is asking to find one or zero match of aa. But here is the output generated



Can someone explain this behavior. Another additional thing, inside the book it is written that the engine backtracks as well. Why is that so?

Thanks,

Ben, My Blog
Norbert Muench
Greenhorn

Joined: Mar 09, 2012
Posts: 19
Been Zaidi wrote:From the console here is the input given


In my opinion, the instruction is asking to find one or zero match of aa. But here is the output generated

No, the ? only binds to the second a, so the pattern that regex is looking for is one 'a' followed by zero or one 'a'
To look for one or zero matches of aa, the regex would be "(aa)?".

Been Zaidi wrote:Another additional thing, inside the book it is written that the engine backtracks as well. Why is that so?

Imagine you have the regex ".*c" and are looking for a match in "abc". Since the * is greedy, the initial match for the beginning of the regex (".*") would be as long as possible, therefore matching the whole string. But then the end of the regex wouldn't fit anymore, so the pattern matching has to backtrack, making .* only match ab instead of abc.
Been Zaidi
Greenhorn

Joined: Mar 21, 2012
Posts: 8

Norbert Muench wrote:
Been Zaidi wrote:From the console here is the input given


In my opinion, the instruction is asking to find one or zero match of aa. But here is the output generated

No, the ? only binds to the second a, so the pattern that regex is looking for is one 'a' followed by zero or one 'a'
To look for one or zero matches of aa, the regex would be "(aa)?".

Been Zaidi wrote:Another additional thing, inside the book it is written that the engine backtracks as well. Why is that so?

Imagine you have the regex ".*c" and are looking for a match in "abc". Since the * is greedy, the initial match for the beginning of the regex (".*") would be as long as possible, therefore matching the whole string. But then the end of the regex wouldn't fit anymore, so the pattern matching has to backtrack, making .* only match ab instead of abc.


Dear Nobert,

I am able to understand the first part of your explanation. It was very helpful. Thanks a lot.

The quoted part, i am a little confused. Can you elaborate it a little simply. Looking forward to hear.

Thanks,
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Example:
.*c means a string with zero or more meta character followed by c.

How about this aaac? Yes. aaa is matched by .*

How about this c? Yes, zero character followed by c.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Helen Ma wrote:Example:
.*c means a string with zero or more meta character followed by c.

How about this aaac? Yes. aaa is matched by .*

How about this c? Yes, zero character followed by c.


I've doubt here, c is already consumed so how it is being count again ??


Tell the difficulties that i am difficult.
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1164
Been Zaidi wrote:
The quoted part, i am a little confused. Can you elaborate it a little simply. Looking forward to hear.



We can look at -



Regards,
Dan


William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1164
Btw, this greedy regex can be converted to a Reluctant one as Greedy, Reluctant, and Possessive shows.

Regards,
Dan
raju salla
Greenhorn

Joined: Jan 05, 2012
Posts: 18
Hello Guyz,
The matcher will search for the atleast one "a". These quantifiers will apply for the exactly one character that means it will find one 'a' and then 0 or 1 a. Then it will find the first 'a' which is based on the criteria one 'a' then 0 or 1 'a'. Then it will find the second match with the 'aa'.

Thanks and Regards
Raju Salla
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confusion Zero Length Match
 
Similar Threads
Java regex?
Regular Expression with java
Regex question
Doubt about Regex and Quantifiers
How to run a java class in console