I have a below regex expression,
/\A(\d{5}[A-Z]{2}[a-zA-Z0-9]{3,7}-TMP|\d{5}[A-Z]{2}\d{3,7}(\-?\d{2})*)\z/
I am checking against below strings. The first and third should return a match I guess and second a no match. But I am not getting a match on all 3. Is my regex wrong?
99844RI1800001
99806CAAUSJ-TMP1
99844RI1800002
Python re
does not support \z
, it supports \Z
as equivalent pattern matching the very end of the string. Your pattern requires the literal z
char to be there at the end of the pattern.
See Rexegg.com reference:
✽ In Python, the token
\Z
does what\z
does in other engines: it only matches at the very end of the string.
Thus you may use
\A(\d{5}[A-Z]{2}[a-zA-Z0-9]{3,7}-TMP|\d{5}[A-Z]{2}\d{3,7}(-?\d{2})*)\Z
See the regex demo
Note that starting with Python 3.6 you would even get an exception:
re.error: bad escape \z at position 68
See Python re
docs:
Changed in version 3.6: Unknown escapes consisting of
'\'
and an ASCII letter now are errors.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With