File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Help me unravel the concept of greedy quantifiers. 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 "Help me unravel the concept of greedy quantifiers." Watch "Help me unravel the concept of greedy quantifiers." New topic
Author

Help me unravel the concept of greedy quantifiers.

gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 923
    
    1

suppose i have a regex as follows

Regex pattern : a? // which means a, 0 or more times. Also this is a greedy quantifier

Now conside the source string as follows :

Source String : aba

On the sun's tutorail on regular expression i read that greedy quantifers will first look for the whole source/input string. if a match is not found , it starts from right. so applying the same concept here if we consider the whole string "aba" clearly there is no match. so now we start from the right , there will be a match at the 2 index. but the actual output of the program is as follows:

I found the text "a" starting at index 0 and ending at index 1
I found the text "" starting at index 1 and ending at index 1.
I found the text "a" starting at index 2 and ending at index 3
I found the text "" starting at index 3 and ending at index 3.

If it is a greed quantifier and as the sun's tutorail as well as KB book points(please refer page 497) that it works the way from right/backwards shouldnt starting character must be found at index 2.
I'm very confused between the working of greedy .kindly help


OCPJP 6(100 %) OCEWCD 6(91 %) OCPJBCD(93%)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18120
    
  39

Please don't create multiple topics -- with different examples, but addressing the same issue. It cause confusion for all parties.

Your other topic...

http://www.coderanch.com/t/573141/java-programmer-SCJP/certification/help-understanding-greedy-reluctant-quantifiers

has a better example. And maybe it would be better to use that one instead.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help me unravel the concept of greedy quantifiers.
 
Similar Threads
Regular Expression in java
Regular Expressions and greedy quantifier
Doubt about Regex and Quantifiers
Regex expressions
Difference Among Greedy, Reluctant, and Possessive Quantifiers