• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Quantifiers and Greedy Quantifiers

 
prashanth kumar
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Matt Russell
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
wise owen
Ranch Hand
Posts: 2023
 
prashanth kumar
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you write the text at that link yourself Wise?
 
Henry Wong
author
Marshal
Pie
Posts: 21024
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
prashanth kumar
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21024
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 2813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you write the text at that link yourself Wise?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic