Checking whole string with a regex

前端 未结 5 1866
醉话见心
醉话见心 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:42

    re.match() always matches from the start of the string (unlike re.search()) but allows the match to end before the end of the string.

    Therefore, you need an anchor: _rex.match(r"\d+$") would work.

    To be more explicit, you could also use _rex.match(r"^\d+$") (which is redundant) or just drop re.match() altogether and just use _rex.search(r"^\d+$").

提交回复
热议问题