aspose file tools*
The moose likes Java in General and the fly likes diff between types of quantifiers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "diff between types of quantifiers" Watch "diff between types of quantifiers" New topic
Author

diff between types of quantifiers

chandana sakiraju
Greenhorn

Joined: Aug 31, 2010
Posts: 3
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Oct 27, 2005
Posts: 19696
    
  20

The Javadoc of java.util.regex.Pattern explains it all.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30537
    
150

Moving to Java in General since this is core Java.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
chandana sakiraju
Greenhorn

Joined: Aug 31, 2010
Posts: 3
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Mar 05, 2008
Posts: 3014
    
  10
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

Joined: Aug 31, 2010
Posts: 3
thanks a lot.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Mar 05, 2008
Posts: 3014
    
  10
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: diff between types of quantifiers