tags:

views:

73

answers:

2

Hi,

I've got a Regex question, I have to recognize tokens in a text that are like:

Foo- followed by either bar or baz followed by - then some numbers, like:

Foo-bar-010
Foo-baz-101

I then want to divide my matches like : Foo-bar -010 and Foo-baz -101

My regex is this one:

(Foo-(bar|baz))-[0-9]+

Which is kinda cool, but I don't want to define a group for the 'bar' or 'baz' clause, since it messes my results.

Any idea to get this result with only one group?

+1  A: 

I think this will do what you want.

This returns the Foo- and -NNN parts as separate groups:

(Foo-ba[rz])(-\d+)

Getting the whole thing back as a single group can be done like this.

(Foo-ba[rz]-\d+)
Mark Biek
Yeah, granted, it sticks with the question's inputs, but my real case problem uses words slightly more distant than bar and baz ;o) thanks anyway.
Vinzz
+6  A: 
(Foo-\b(?:bar|baz)\b)-[0-9]+

?: usually flags the group as a non-capturing match (depending on your engine).

erikkallen
Thanks, didn't know that feature.
Vinzz
Excellent. That's a new one to me too.
Mark Biek