How to combine these two regular expressions?

I would like to combine the following two regular expressions into one:

  • ^([AZ]{1,2}) ?([0-9]{1,4})$ for example. AB 1234
  • ^([0-9]{1,4}) ?([AZ]{1,2})$ for example. 1234 AB

I thought it would be simple:

^([AZ]{1,2}) ?([0-9]{1,4})|([0-9]{1,4}) ?([AZ]{1,2})$

However, the above always returns 4 groups, e.g.

  • ''
  • ''
  • '1234'
  • 'AB'

How can I combine these two regular expressions so that two groups always return? eg.

  • '1234'
  • 'AB'

or

  • 'AB'
  • '1234'
+4
source share
1 answer

Firstly, your combination is not entirely correct, because ^ applies only to the first alternative, and $ applies only to the second. So you need to group the rotation:

 ^(?:([AZ]{1,2}) ?([0-9]{1,4})|([0-9]{1,4}) ?([AZ]{1,2}))$ 

Now, what you want to achieve cannot be done with all the regex mechanisms, but some (like PCRE) support a special interlace design in which capture groups are counted separately for all interlaces. This is the syntax:

 ^(?|([AZ]{1,2}) ?([0-9]{1,4})|([0-9]{1,4}) ?([AZ]{1,2}))$ 

EDIT:

Unfortunately, this is not particularly supported by Python. There is also no alternative to reusing the named capture group. Therefore, you may have to filter out empty lines from match.groups() or stick with two regular expressions.

+3
source

All Articles