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

Quantifiers and Greedy Quantifiers

prashanth kumar
Ranch Hand

Joined: Mar 22, 2004
Posts: 162
Hello Ranchers,
Is there any difference between Quantifiers and greedy quantifiers OR are all quantifiers Greedy Quantifiers?

Sorry if its too basic question but iam literally confused.

Thanks
Prashanth


SCJP1.5(86%)<br />SCWCD1.4(95%)<br />SCBCD1.3(92%)<br />IBM 252
Matt Russell
Ranch Hand

Joined: Aug 15, 2006
Posts: 165
I presume you're referring to regular expressions. There is a difference: there are non-greedy quantifiers such as "??", "*?" and "+?", in contrast to greedy quantifiers like "?", "*" and "+". According to K&B, only greedy quantifiers are examined in SCJP 5.


Matt
Inquisition: open-source mock exam simulator for SCJP and SCWCD
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
What are the differences among greedy, reluctant and possessive quantifiers?
prashanth kumar
Ranch Hand

Joined: Mar 22, 2004
Posts: 162
Thanks for the URLs.
Also iam getting bit scared as my exam is within 2 weeks away and i have read some basics ..Anyways i will give it a go.I dont have much time.I have allocated only 1 month for the exam.So fingers crossed.I hope i pass the exam;-);-)

Cheers
Prashanth
prashanth kumar
Ranch Hand

Joined: Mar 22, 2004
Posts: 162
Hello,
I have again got this confusion..
How can i ever find out when is a quantifier becomes greedy
ex:
1)proj([^,])* --> is a quantifier

2).*xx --> is a greedy quantifier


can anyone advice?

Thanks
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
Did you write the text at that link yourself Wise?


SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18707
    
  40

How can i ever find out when is a quantifier becomes greedy


A qualifier, by default, is greedy. When you see the qualifier followed by a "?", then it is declared as reluctant.

In both of your examples, neither "*" qualifier is followed by a "?".

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
prashanth kumar
Ranch Hand

Joined: Mar 22, 2004
Posts: 162
Originally posted by Henry Wong:




In both of your examples, neither "*" qualifier is followed by a "?".

Henry


So according to it,given the following text
"proj3.txt,proj1sched.pdf,proj1,proj2,proj1.java"

and pattern

proj1([^,])*

why doesnt it match the whole text..It acts as a reluctant and matches

proj1sched.pdf
proj1
proj1.java


Many Thanks
Prashanth
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18707
    
  40


So according to it,given the following text
"proj3.txt,proj1sched.pdf,proj1,proj2,proj1.java"

and pattern

proj1([^,])*

why doesnt it match the whole text..It acts as a reluctant and matches

proj1sched.pdf
proj1
proj1.java



Just because the regex doesn't match the "whole text" doesn't make the qualifier "reluctant". In this case, the "greedy" qualifier still has to obey what is being qualified ([^,]) and can't match anything with commas.

If you change the qualifier in the pattern to reluctant:

proj1([^,])*?

You will see that each find() will try to match as little as possible. And the matches are...

proj1
proj1
proj1


Hope this helps,
Henry
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
Did you write the text at that link yourself Wise?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by Marcus Green:
Did you write the text at that link yourself Wise?


Not unless his real name is Anant Athale.

Quoting from xyzws.com:

The XyzWS.com is hosted by a group of hardworking Renjings who call themselves the Tigons.


Indeed.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Quantifiers and Greedy Quantifiers