java.util.regex and multiple matches

Discussion in 'Java' started by argabalala@yahoo.fr, Sep 27, 2005.

  1. Guest

    Hi,

    I'm trying to catch all the values matched by subgroups
    of the following regex (it's a simplified version, x and y
    originally are regex as well).


    (?:(?:(x)|(y)),)*(x)(?:,(?:(x)|(y)))*|(a)

    This expresion means wether :
    -I must get an "x" somewhere, and a set (perhaps several) of "x" and
    "y"
    eventually empty.
    -I get a "a"

    Sadly, I only catch the last elements that matched the
    subgroups. Hence, I loose some "x" and some "y".
    Therefore, I use a trick today to do the job :
    I globally match the expression using java.util.regex
    and then I split the string and match resulting individual
    elements to "x", "y" and "a".

    So I'm certainly doing the job done twice, because the
    first global match must have parsed each "x", "y" and "a"
    individually.

    Is there a better way to acheive this with java.util.regex ?
    Or with any other robust package ?

    Thanks for your help,
    --
    arga
    , Sep 27, 2005
    #1
    1. Advertising

  2. Roedy Green Guest

    On 27 Sep 2005 08:03:00 -0700, wrote or quoted :

    >I'm trying to catch all the values matched by subgroups
    >of the following regex (it's a simplified version, x and y
    >originally are regex as well).


    Adding more () is the first thing to try.

    See http://mindprod.com/jgloss/regex.html
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
    Roedy Green, Sep 27, 2005
    #2
    1. Advertising

  3. Roedy Green <> wrote:
    > On 27 Sep 2005 08:03:00 -0700, wrote or quoted :
    >
    >> I'm trying to catch all the values matched by subgroups
    >> of the following regex (it's a simplified version, x and y
    >> originally are regex as well).

    >
    > Adding more () is the first thing to try.


    This doesn't help with repetition operators; you always get either one or
    all of the repeated parts but not all individually. IMHO the OP's approach
    is the best he can do: match the overall regexp and then use a second regexp
    to match all occurrences of something in a group.

    Kind regards

    robert
    Robert Klemme, Sep 27, 2005
    #3
  4. Robert Klemme wrote:
    > Roedy Green <> wrote:
    >
    >> On 27 Sep 2005 08:03:00 -0700, wrote or quoted :
    >>
    >>> I'm trying to catch all the values matched by subgroups
    >>> of the following regex (it's a simplified version, x and y
    >>> originally are regex as well).

    >>
    >>
    >> Adding more () is the first thing to try.

    >
    >
    > This doesn't help with repetition operators; you always get either one
    > or all of the repeated parts but not all individually. IMHO the OP's
    > approach is the best he can do: match the overall regexp and then use a
    > second regexp to match all occurrences of something in a group.


    Yes, and with that being the case I'd write the regex itself like this:

    a|(?(?:[xy],)*x(?:,y)*)

    It matches exactly the same strings and is much simpler. With fewer
    alternations and the fixed alternative first it may also run a bit
    faster, if that happened to be an issue. You can always get the matched
    input subsequence from the Matcher's group(0), if you happen to be
    accepting subsequence matches (Matcher.find(), Matcher.lookingAt()), so
    you don't need to group the whole pattern. There might be more pattern
    optimizations available if this were really performance critical; I
    applied only those that also simplify and clarify the pattern.


    --
    John Bollinger
    John C. Bollinger, Sep 28, 2005
    #4
  5. Guest

    Robert Klemme a écrit :

    > IMHO the OP's approach
    > is the best he can do: match the overall regexp and then use a second regexp
    > to match all occurrences of something in a group.

    I'm disappointed to see my approach confirmed to be a valid one :)

    Thanks to all posters for their comments.

    --
    arga
    , Sep 28, 2005
    #5
    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. dhek bhun kho

    java.util.regex: Backreferences?

    dhek bhun kho, Jul 9, 2003, in forum: Java
    Replies:
    2
    Views:
    775
    dhek bhun kho
    Jul 9, 2003
  2. Roedy Green

    Re: java.util.regex.Matcher

    Roedy Green, Jul 30, 2003, in forum: Java
    Replies:
    0
    Views:
    363
    Roedy Green
    Jul 30, 2003
  3. Calum MacLean

    java.util.regex question

    Calum MacLean, Nov 28, 2003, in forum: Java
    Replies:
    3
    Views:
    410
    John C. Bollinger
    Dec 1, 2003
  4. Talin
    Replies:
    3
    Views:
    464
    Talin
    Nov 19, 2005
  5. joes
    Replies:
    2
    Views:
    991
    Daniel Pitts
    May 25, 2007
Loading...

Share This Page