Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2005

Tip: Looking for answers? Try searching our database.

java.util.regex and multiple matches

Thread view: 
argabalala@yahoo.fr - 27 Sep 2005 16:03 GMT
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,
Signature

arga

Roedy Green - 27 Sep 2005 20:03 GMT
>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
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Robert Klemme - 27 Sep 2005 21:47 GMT
>> 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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.