The “\z” anchor not working in Python regex

后端 未结 1 426
被撕碎了的回忆
被撕碎了的回忆 2021-01-22 18:22

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 s

相关标签:
1条回答
  • 2021-01-22 18:31

    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.

    0 讨论(0)
提交回复
热议问题