Checking whole string with a regex

前端 未结 5 1843
醉话见心
醉话见心 2020-11-22 08:34

I\'m trying to check if a string is a number, so the regex \"\\d+\" seemed good. However that regex also fits \"78.46.92.168:8000\" for some reason, which I do not want, a l

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 08:53

    \Z matches the end of the string while $ matches the end of the string or just before the newline at the end of the string, and exhibits different behaviour in re.MULTILINE. See the syntax documentation for detailed information.

    >>> s="1234\n"
    >>> re.search("^\d+\Z",s)
    >>> s="1234"
    >>> re.search("^\d+\Z",s)
    <_sre.SRE_Match object at 0xb762ed40>
    

提交回复
热议问题