• 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

Regex help

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Please tell me how combination of these quantifiers works:

import java.util.regex.*;
class Regex1
{
public static void main(String[] args)
{
Pattern p = Pattern.compile("\\d*+");
Matcher m = p.matcher("ab34ef");

while(m.find()){
System.out.println(m.start() +" "+m.group());
}
}
}

As i know that:
* -------> zero or more occurrence
+ -------> one or more occurrence
? -------> Zero or one occurrence

suppose my input string is "ab34ef" and comparing string is "\\d*","\\d+" and "\\d?" then i will get the output if i put these comparision in the above code:

for "\\d*" -------output will be 01234456
for "\\d+" -------output will be 234
for "\\d?" -------output will be 012334456

But what will be the output if i use comaprision string like "\\d*+","\\d*?".
I am not able to draw conclusion from the output ,how exactly combinations of these quantifier working.
Please help.

Also please explain me the exact difference between greedy, reluctant and possessive quantifiers with some real example.



[ January 09, 2008: Message edited by: Hamraj Kulshreshtha ]
[ January 09, 2008: Message edited by: Hamraj Kulshreshtha ]
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone please answer this, we are really confused and need help.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Also please explain me the exact difference between greedy, reluctant and possessive quantifiers with some real example.



A qualifier is something like "?", "*", or "+", which is used to qualify a pattern. So... "A+" means one or more of the letter "A".


By greedy, it means that it will match as much as possible. This is not that obvious. For example, if the string has 5 "A" is a row, you would think that it will always match 5 A's. But remember, this may be part of a much larger pattern such as "ABCA+ABC".

If the greedy qualifier takes all of the A's, then the rest of the pattern will fail. So the pattern will be greedy and take all 5 A's, but if it causes a match to fail, then it will backtrack and take only 4, then 3, etc.


By reluctant, it will only take 1 A's. And again, this is not that obvious. If only taking one A will cause the rest of the match to fail, it will take 2 A's, then 3, etc., until it matches or takes all of the A's.


Possessive is a special case of greedy. It just means that you will be greedy and it won't backtrack. Instead, it will fail the match rather than to backtrack. This is only used if you know that backtrack is not necessary -- as removing backtracking supposedly makes matching faster.

Henry
 
Dean Jones
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry, but can you suggest some place where we can find more examples of this, so that we have a better understanding.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suggestion:
http://java.sun.com/docs/books/tutorial/essential/regex/quant.html


Yours,
Bu.
 
reply
    Bookmark Topic Watch Topic
  • New Topic