aspose file tools*
The moose likes Beginning Java and the fly likes Reluctant Quantifier Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reluctant Quantifier" Watch "Reluctant Quantifier" New topic
Author

Reluctant Quantifier

Pawan Arora
Ranch Hand

Joined: Sep 14, 2008
Posts: 105
Hi,
can anyone please tell me the meaning of Reluctant quantifier in regex?
As from me, it's match reluctantly match an input String 1 or more times. But in expression below


I don't understand why it's matching the string with the pattern? from me it shouldn't have been so.

Thanks in advance.

[ November 26, 2008: Message edited by: Pawan Arora ]

[ November 26, 2008: Message edited by: Pawan Arora ]
[ November 26, 2008: Message edited by: Pawan Arora ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38910
    
  23
Have you read the Java� Tutorials?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18855
    
  40

The reluctant qualifier serves no purpose here. The match would be the same regardless of whether the qualifier is greedy, reluctant, or possessive.

I don't understand why it's matching the string with the pattern? from me it shouldn't have been so.


Why don't you think this should match?

[EDIT: sorry, didn't see the space. The match should fail -- find() should return no match]

Henry
[ November 26, 2008: Message edited by: Henry Wong ]

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

Joined: Sep 14, 2008
Posts: 105
Why the following not returning any output?
Pattern p = Pattern.compile("aa*? ");
Matcher m = p.matcher("aaaa");
if (m.find())
System.out.println(m.group());

Don't you think it's because of having a reluctant pattern?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18855
    
  40

Don't you think it's because of having a reluctant pattern?


It's not matching because your pattern has a trailing space, and your string doesn't. Since you string doesn't have the space, there is no way for it to match.

This has nothing to do with the reluctant qualifier portion of the pattern.

Henry
Pawan Arora
Ranch Hand

Joined: Sep 14, 2008
Posts: 105
Originally posted by Henry Wong:




This has nothing to do with the reluctant qualifier portion of the pattern.

Henry


So how would I differentiate Reluctant, Greedy and Possessive quantifier?
As I know the symbol to denote them, but doesn't know thier defination well besides Greedy.
[ November 26, 2008: Message edited by: Pawan Arora ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18855
    
  40

Originally posted by Pawan Arora:

So how would I differentiate Reluctant, Greedy and Possessive quantifier?
As I know the symbol to denote them, but doesn't know thier defination well besides Greedy.



If you don't know how qualifiers work, then I recommend that you follow Campbell's advice, and start with the tutorials.

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18855
    
  40

But to answer your question about the behavior...

A greedy qualifier would mean to ... match as much as possible, but will back off of it (matching less), if matching too much causes the overall match to fail.

A reluctant qualifier would mean to ... match as little as possible, but will match more, if matching too little causes the overall match to fail.

A possessive qualifier would mean to ... match as much as possible, and do not back off (matching less), even if it causes the overall match to fail.

Henry
[ November 26, 2008: Message edited by: Henry Wong ]
Pawan Arora
Ranch Hand

Joined: Sep 14, 2008
Posts: 105
Thanks Sir!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reluctant Quantifier