Skip to content Skip to sidebar Skip to footer

The "\z" Anchor Not Working In Python Regex

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

Solution 1:

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.

Post a Comment for "The "\z" Anchor Not Working In Python Regex"