• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

diff between types of quantifiers

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Javadoc of java.util.regex.Pattern explains it all.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Master Rancher
Posts: 4796
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot.
 
Mike Simmons
Master Rancher
Posts: 4796
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Master Rancher
Posts: 4796
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Get me the mayor's office! I need to tell her about this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic