Either|Or part of regex for matching two possibilities

2.4k Views Asked by At

We have two ways of referring to the same system:

  • tr12345
  • fr12345

Right now I have one regular expression per variation, e.g

  1. /(?:^|^\s|[^\/a-zA-Z0-9])tr([0-9]+).*$/img
  2. /(?:^|^\s|[^\/a-zA-Z0-9])fr([0-9]+).*$/img

This works fine, but I thought it could be more efficient by combining the two. I saw from other articles that you can use the alternation | to match variations. Do I need to repeat the regex after the "tr" if I'm trying to match both? E.g is something like this possible?

/(?:^|^\s|[^\/a-zA-Z0-9])tr|fr([0-9]+).*$/img

Example input1:

tr12345

Desired output1:

12345

Example input2:

fr123456

Desired output2:

123456

I've been playing with it here: https://regex101.com/r/FUCmv0/2

1

There are 1 best solutions below

3
On BEST ANSWER

You're trying to do it in a wrong way. '|' is actually OR, and have low priority, so your regex is ...tr OR fr..., but you need ...(tr|fr)...

So you should use () (like in math expressions). And regex will be

/(?:^|^\s|[^\/a-zA-Z0-9])(tr|fr)([0-9]+).*$/img

Note that () also produces match groups.

But in your case next expression will be better:

/(?:^|^\s|[^\/a-zA-Z0-9])[tf]r([0-9]+).*$/img

Here [tf] means "one of [t,r]"