Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to recognize two different word in a regex without grouping

Tags:

regex

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?

like image 598
Vinzz Avatar asked Dec 22 '22 09:12

Vinzz


1 Answers

(Foo-\b(?:bar|baz)\b)-[0-9]+

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

like image 180
erikkallen Avatar answered May 20 '23 13:05

erikkallen