Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > With regex, accessing multiple groups under quantifiers

Reply
Thread Tools

With regex, accessing multiple groups under quantifiers

 
 
valan.wood@gmail.com
Guest
Posts: n/a
 
      09-09-2007
In http://www.javaregex.com/RegexRecipesV1.pdf, I found a nifty little
example which illustrates what I'm trying to do:

Pattern p = Pattern.compile("(?i)((apple|orange|banana)[\\s,]*)+");
String txt = "List some fruits: apple, orange, banana";
Matcher m = p.matcher(txt);
boolean found = m.find();
System.out.println(m.group());
// Prints "apple, orange, banana"
System.out.println(m.group(1));
// Prints "banana"
System.out.println(m.group(2));
// Prints "banana"

Notice how it grabs "banana", but not "apple" or "orange". I'm
thinking it should match all three, for a total of 6 groups. Is there
a way to match all six without introducing a while (m.find()) loop?

 
Reply With Quote
 
 
 
 
Joshua Cranmer
Guest
Posts: n/a
 
      09-09-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> In http://www.javaregex.com/RegexRecipesV1.pdf, I found a nifty little
> example which illustrates what I'm trying to do:
>
> Pattern p = Pattern.compile("(?i)((apple|orange|banana)[\\s,]*)+");
> String txt = "List some fruits: apple, orange, banana";
> Matcher m = p.matcher(txt);
> boolean found = m.find();
> System.out.println(m.group());
> // Prints "apple, orange, banana"
> System.out.println(m.group(1));
> // Prints "banana"
> System.out.println(m.group(2));
> // Prints "banana"
>
> Notice how it grabs "banana", but not "apple" or "orange". I'm
> thinking it should match all three, for a total of 6 groups. Is there
> a way to match all six without introducing a while (m.find()) loop?
>

Why, oh why, don't people bother to read documentation these days? I
will quote to you directly from the JavaDocs for java.util.regex.Pattern

The captured input associated with a group is always the subsequence
that the group *most recently matched.* If a group is evaluated a second
time because of quantification then its previously-captured value, if
any, will be retained if the second evaluation fails. [ Emphasis added ]

The short answer to your question is: no.
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Resolving Quantifiers of Sub-Expression Quantifiers: ie: /( ( ( foo\w* )+? | ( fle\w+)* ){0,5} ) )? bar/xg; ? sln@netherlands.com Perl Misc 0 02-18-2009 12:48 AM
JavaScript RegExp Quantifiers Nathan Sokalski ASP .Net 2 06-13-2008 07:51 AM
Question about Quantifiers in java Regular expression NeoGeoSNK Java 4 03-03-2008 09:49 PM
Greedy and non greedy quantifiers Dan Kelly Ruby 4 01-19-2008 08:36 PM
rexular expression quantifiers Johnathan Smith Ruby 4 01-07-2008 07:33 PM



Advertisments