Reputation: 3898
I want a regex that will match for strings of RACE
or RACE_1
, but not RACE_2
and RACE_3
. I've been on Rubular for a while now trying to figure it out, but can't seem to get all the conditions I need met. Help is appreciated.
Upvotes: 1
Views: 473
Reputation: 4010
You can use:
(\bRACE(_[1])?\b)
It requires the one copy of RACE
, and then 0 -> N
occurrences of the _[1]
. In the square brackets you can include any number you want. EXAMPLE:
(\bRACE(_[12345])?\b)
will match up to RACE_5
. You can then customize it to even skip numbers if you want [1245]
for RACE_1, RACE_2, RACE_4, RACE_5
but not RACE_3
.
Upvotes: 2
Reputation: 768
/RACE(?!_)|RACE_1/
Its a bit of a hack but might fit your needs
EDIT:
Here might be a more specific one that works better
/RACE(?!_\d)|RACE_1/
In both cases, you use negative lookahead to enforce that RACE cannot be followed by _
and a number, but then specifically allow it with the or statement following.
Also, if you plan on only searching for instances of said matches that are whole words, prepend/append with \b to designate word boundaries.
/\bRACE(?!_\d)|RACE_1\b/
Upvotes: 1
Reputation: 859
RACE(_1)?\b
\b means the end of a word, and that prevents matching RACE in RACE_2.
Upvotes: 3