0

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

Tiago
  • 1,984
  • 1
  • 21
  • 43

1 Answers1

1

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]"

  • Interesting, thanks. Although what would happen in scenarios where there's a variation more than one letter? E.g tr12345 & kn12345? The square brackets only match single characters, right? Or can I group two characters into OR expressions? I might be overcomplicating... – Tiago Oct 17 '17 at 13:52
  • @Tiago , OR works fine in this case. For example, /ab(aca|cde)ba/ will match both abacaba & abcdeba with match groups 'aca' or 'cde' – Валерий Маевский Oct 17 '17 at 13:59
  • 1
    Thanks! This seems to be working, including making this a non-capturing group: `/(?:^|^\s|[^\/a-zA-Z0-9])(?:tr|fr)([0-9]+).*$/img` – Tiago Oct 17 '17 at 14:17