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.
source share