Python 使用正则表达式检查整个字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3994493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:45:24  来源:igfitidea点击:

Checking whole string with a regex

pythonregex

提问by dutt

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 little bit of code:

我试图检查一个字符串是否是一个数字,所以正则表达式 "\d+" 看起来不错。但是,由于某种原因,该正则表达式也适合“78.46.92.168:8000”,这是我不想要的,有一点代码:

class Foo():
    _rex = re.compile("\d+")
    def bar(self, string):
         m = _rex.match(string)
         if m != None:
             doStuff()

And doStuff() is called when the ip adress is entered. I'm kind of confused, how does "." or ":" match "\d"?

并且在输入 IP 地址时调用 doStuff()。我有点困惑,“。”怎么办?或“:”匹配“\d”?

采纳答案by eumiro

\d+matches any positive number of digits withinyour string, so it matches the first 78and succeeds.

\d+匹配的数字任意正数范围内的字符串,因此它匹配的第一个78和成功。

Use ^\d+$.

使用^\d+$.

Or, even better: "78.46.92.168:8000".isdigit()

或者,甚至更好: "78.46.92.168:8000".isdigit()

回答by prostynick

Change it from \d+to ^\d+$

将其更改\d+^\d+$

回答by Tim Pietzcker

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.

re.match()始终从字符串的开头匹配(与 不同re.search()),但允许匹配在字符串结尾之前结束。

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

因此,您需要一个锚点:_rex.match(r"\d+$")会起作用。

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+$").

更明确地说,您也可以使用_rex.match(r"^\d+$")(这是多余的)或完全删除re.match()并使用_rex.search(r"^\d+$").

回答by ghostdog74

\Zmatches 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 documentationfor detailed information.

\Z匹配字符串的结尾而$匹配字符串的结尾或刚好在字符串结尾的换行符之前,并且在re.MULTILINE. 有关详细信息,请参阅语法文档

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

回答by Wiktor Stribi?ew

There are a couple of options in Python to match an entire input with a regex.

Python 中有几个选项可以将整个输入与正则表达式匹配。

Python 2 and 3

Python 2 和 3

In Python 2 and 3, you may use

在 Python 2 和 3 中,您可以使用

re.match(r'\d+$') # re.match anchors the match at the start of the string, so $ is what remains to add

or - to avoid matching before the final \nin the string:

或 - 避免\n在字符串中的最后一个之前匹配:

re.match(r'\d+\Z') # \Z will only match at the very end of the string

Or the same as above with re.searchmethod requiring the use of ^/ \Astart-of-string anchor as it does not anchor the match at the start of the string:

或者与上面相同的re.search方法需要使用^/ \Astart-of-string 锚,因为它不会在字符串的开头锚定匹配:

re.search(r'^\d+$')
re.search(r'\A\d+\Z')

Note that \Ais an unambiguous string start anchor, its behavior cannot be redefined with any modifiers (re.M/ re.MULTILINEcan only redefine the ^and $behavior).

注意\A是一个明确的字符串起始锚,它的行为不能用任何修饰符重新定义(re.M/re.MULTILINE只能重新定义^$行为)。

Python 3

蟒蛇 3

All those cases described in the above section and one more useful method, re.fullmatch(also present in the PyPi regexmodule):

上一节中描述的所有情况以及一种更有用的方法re.fullmatch(也存在于PyPiregex模块中):

If the whole stringmatches the regular expression pattern, return a corresponding match object. Return Noneif the string does not match the pattern; note that this is different from a zero-length match.

如果整个字符串与正则表达式pattern匹配,则返回相应的匹配对象。None如果字符串与模式不匹配,则返回;请注意,这与零长度匹配不同。

So, after you compile the regex, just use the appropriate method:

因此,在编译正则表达式后,只需使用适当的方法:

_rex = re.compile("\d+")
if _rex.fullmatch(s):
    doStuff()