I need a regular expression, which catches a first expression. If its not found, catch the second one.
The first one is a 2-4 long number with a following ‘X’, if it’s not found, just catch the 2-4 long number without ‘X’.
foo bar 321 string 1234X and so on // catch 1234X
I found a short example here (a)?b(?(1)c|d) but i misinterpreted it.
(\d{2,4}X)?(?(1)(\d{2,4})X|\D(\d{2,4})\D)
It always finds the ‘321’. I tried several variations, but nothing works.
You could use:
(Quote and escape it properly before use.)
Note that it will match
1111Xin1111111111111111111X, and also if the number is part of “words”. If you don’t want that use something like:Perl demo: