How to recognize two different words in a regular expression without grouping

I have a Regex question, I have to recognize tokens in a text that looks like:

Foo-Followed by either bar, or bazfollowed -, then some of the figures, for example:

Foo-bar-010
Foo-baz-101

Then I want to split my matches like this: Foo-bar -010andFoo-baz -101

My regex is as follows:

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

This is cool, but I don’t want to define a group for the sentence β€œbar” or β€œbaz”, as it negatively affects my results.

Any idea to get this result with just one group?

+3
source share
2 answers
(Foo-\b(?:bar|baz)\b)-[0-9]+

?: Usually groups a group as a non-conceptual match (depending on your engine).

+8

, , .

Foo- -NNN :

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

.

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

Source: https://habr.com/ru/post/1715412/


All Articles