
Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
>> 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
John C. Bollinger - 28 Sep 2005 03:24 GMT
>>> I'm trying to catch all the values matched by subgroups
>>> of the following regex (it's a simplified version, x and y
[quoted text clipped - 6 lines]
> 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.

Signature
John Bollinger
jobollin@indiana.edu
argabalala@yahoo.fr - 28 Sep 2005 10:35 GMT
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.

Signature
arga