With regex, accessing multiple groups under quantifiers

Discussion in 'Java' started by valan.wood@gmail.com, Sep 9, 2007.

  1. Guest

    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?
    , Sep 9, 2007
    #1
    1. Advertising

  2. 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
    Joshua Cranmer, Sep 9, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. NeoGeoSNK
    Replies:
    4
    Views:
    3,657
    Joshua Cranmer
    Mar 3, 2008
  2. Nathan Sokalski

    JavaScript RegExp Quantifiers

    Nathan Sokalski, Jun 11, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    345
    Hans Kesting
    Jun 13, 2008
  3. Wolfgang Thomas
    Replies:
    16
    Views:
    203
    Xicheng
    Mar 9, 2006
  4. Amy Lee
    Replies:
    3
    Views:
    387
    Amy Lee
    Oct 24, 2008
  5. Replies:
    0
    Views:
    132
Loading...

Share This Page