Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The "\z" anchor not working in Python regex

Tags:

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 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
like image 503
themaster Avatar asked Sep 18 '25 07:09

themaster


1 Answers

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.

like image 129
Wiktor Stribiżew Avatar answered Sep 20 '25 21:09

Wiktor Stribiżew