Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

diff between types of quantifiers

 
chandana sakiraju
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am new to java.can you please post me difference between quantifiers
eg:
if i give(.*?aa) then what is result
if(.*+aa)
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

Quantifiers are discussed both in the regular expression Javadocs and in Oracle's regular expression tutorials (and a million other places on the web)--which sites have you looked at for answers so far?
 
Rob Spoor
Sheriff
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Javadoc of java.util.regex.Pattern explains it all.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34837
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Java in General since this is core Java.
 
chandana sakiraju
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,so far i referred in google,and Scjp(exam 310-065) goide,but even i was confused with quantifiers.i need explanation.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you read the link Rob provided? Note that in your original example you already *have* a quantified, the asterisk *... I'm not sure why you'd use a ? Or + after it--what happened when you tried it?
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:Did you read the link Rob provided? Note that in your original example you already *have* a quantified, the asterisk *... I'm not sure why you'd use a ? Or + after it

Umm, those are addressed in the link Rob provided. Plain * is a greedy quantifier, 0 or more times. *? is a reluctant quantifier, zero or more times. *+ is a possessive quantifier, zero or more times.

What's missing from the JavaDoc, of course, is any decent explanation of the differences between greedy, reluctant, and possessive. Instead we are referred to Mastering Regular Expressions, 3nd Edition, Jeffrey E. F. Friedl. A great book, but not readily available online, at least not for free. The truth is, the JavaDoc kind of sucks here. Back when I learned JDK 1.4 regex, there was no real alternative online, you just had to buy the book (2nd edition then). Now, however, there are decent online explanations available.

Chandana, I recommend looking that the Java tutorial, Differences Among Greedy, Reluctant, and Possessive Quantifiers

David Newton wrote:what happened when you tried it?

Always good advice, especially for code where one asks "what is the result?" Chandana, you should have been able to discover the result pretty easily here by simply running the code. A better question is why do you get the results you do. That's answered, I hope, in the tutorial link I gave.
 
chandana sakiraju
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, after looking more at the Java tutorial link I gave, I decided it isn't very good. It's OK for describing .*, .*? and .*+, but it's poor (and sometimes flat-out wrong) at generalizing that to other expressions like a*+ or [a-zA-Z]*?. It would probably be better to look here:

Repetition with Star and Plus
Possessive Quantifiers

This site is not specifically about Java regular expressions, but it's the best general regular expressions site I've seen.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:
David Newton wrote:Did you read the link Rob provided? Note that in your original example you already *have* a quantified, the asterisk *... I'm not sure why you'd use a ? Or + after it

Umm, those are addressed in the link Rob provided. Plain * is a greedy quantifier, 0 or more times. *? is a reluctant quantifier, zero or more times. *+ is a possessive quantifier, zero or more times.

Umm, I actually assumed the original poster had made an error, since he didn't mention reluctant qualifiers and didn't seem really up on regexes at all--I was more prompting for an explanation of what he *thought* he was doing.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the title was "diff between types of quantifiers", followed by an example using three different types of quantifiers and an input that would be good for exploring exactly that. I'd give him the benefit of doubt here.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic