aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes greedy quantifier concept doubt 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 "greedy quantifier concept doubt" Watch "greedy quantifier concept doubt" New topic
Author

greedy quantifier concept doubt

saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

How it is working, i am not getting. .Any good rule so i can evaluate any expression easily?

C:\SCJP\Chapter6\Regex>java Regex1 "yyxx xxyyx" "y*xx"
0
5

C:\SCJP\Chapter6\Regex>java Regex1 "yyxxxxyyx" "y*xx"
0
4


Tell the difficulties that i am difficult.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4490
    
    8

I'm assuming Regex1 takes the second argument as a regular expression, and finds the starting positions of it within the first string?

OK, here's what's happening. y*xx translates as "find any number of consecutive y characters, including zero, followed by xx".

So in the first case, it finds a match straught away: "yyxx". Then it starts looking again immediately after that. The next match is just "xx", with zero 'y's. The second case is identical, but you've removed the space so the second group it finds is one place further left.

saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

I thought it will seek from backward as book says

The greedy quantifier does in fact read the entire source data, and then it works backwards (from the right) until it finds the rightmost match. At that point, it includes everything from earlier in the source data up to and including the data that is part of the rightmost match.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

Saloni i faced lot of trouble regarding regular expression from the kb6 book. the oracle tutorial are the best. check out the link http://docs.oracle.com/javase/tutorial/essential/regex/quant.html[/url
also you can check this thread http://www.coderanch.com/t/573141/java-programmer-SCJP/certification/help-understanding-greedy-reluctant-quantifiers
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

saloni jhanwar wrote:I thought it will seek from backward as book says

The greedy quantifier does in fact read the entire source data, and then it works backwards (from the right) until it finds the rightmost match. At that point, it includes everything from earlier in the source data up to and including the data that is part of the rightmost match.



Given the context of the quote (or the lack of it), it is clearly incorrect -- as you can see from your example. I am thinking that from the description that you quoted, it was for a book example that had a greedy qualifier for a dot. For that case, it can "read the entire source data and then work backwards".

Henry

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4490
    
    8

I think what they're getting at is this.

Consider the string abcyyyyyyxx, and the regular expression y*xx

Now, if we're looking to match the regex against part of the string, there are actually a few possibilities:
xx
yxx
yyxx
yyyxx
...
yyyyyyxx


Because it's a greedy quantifier, it will match the largest chunk that it can - the last one. I don't know the details about how it actually does that, but I suspect it involves finding the match and then searching backwards to find out how big a match it can get. I think that's what the quote is getting at.

But the program you're using to find matches is searching left-to-right. Are you using Matcher.find()? The specification for that says:

Attempts to find the next subsequence of the input sequence that matches the pattern.

This method starts at the beginning of this matcher's region, or, if a previous invocation of the method was successful and the matcher has not since been reset, at the first character not matched by the previous match.

And remember that this specification is for all regular expressions that it might be given. The method spec has to be the same for greedy and non-greedy quantifiers.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Thanks Matthew,Henry,Gurpeet
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: greedy quantifier concept doubt