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”?
5 个解决方案
#1
23
\d+
matches any positive number of digits within your string, so it matches the first 78
and succeeds.
\ d +匹配字符串中的任何正数位数,因此它与前78个匹配并成功。
Use ^\d+$
.
Or, even better: "78.46.92.168:8000".isdigit()
或者,甚至更好:“78.46.92.168:8000”.isdigit()
#2
12
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 + $”) 。
#3
7
\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.
\ Z匹配字符串的结尾,而$匹配字符串的结尾或恰好在字符串末尾的换行符之前,并且在re.MULTILINE中表现出不同的行为。有关详细信息,请参阅语法文档。
>>> s="1234\n">>> re.search("^\d+\Z",s)>>> s="1234">>> re.search("^\d+\Z",s)<_sre.SRE_Match object at 0xb762ed40>
#4
5
Change it from \d+
to ^\d+$
将其从\ d +更改为^ \ d + $
#5
5
There are a couple of options in Python to match an entire input with a regex.
Python中有几个选项可以将整个输入与正则表达式进行匹配。
Python 2
In Python 2.x, you may use
在Python 2.x中,您可以使用
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 \n
in 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.search
method requiring the use of ^
/ \A
start-of-string anchor as it does not anchor the match at the start of the string:
或者与上面相同的re.search方法需要使用^ / \一个字符串起始锚点,因为它不会在字符串的开头锚定匹配:
re.search(r'^\d+$')re.search(r'\A\d+\Z')
Note that \A
is an unambiguous string start anchor, its behavior cannot be redefined with any modifiers (re.M
/ re.MULTILINE
can only redefine the ^
and $
behavior).
注意\ A是一个明确的字符串起始锚点,它的行为不能用任何修饰符重新定义(re.M / re.MULTILINE只能重新定义^和$行为)。
Python 3
All those cases described in the Python 2 section and one more useful method, re.fullmatch
(also present in the PyPi regex
module):
所有这些在Python 2部分中描述的情况和一个更有用的方法re.fullmatch(也出现在PyPi正则表达式模块中):
If the whole string matches the regular expression pattern, return a corresponding match object. Return
None
if the string does not match the pattern; note that this is different from a zero-length match.如果整个字符串与正则表达式模式匹配,则返回相应的匹配对象。如果字符串与模式不匹配,则返回None;请注意,这与零长度匹配不同。
So, after you compile the regex, just use the appropriate method:
因此,在编译正则表达式之后,只需使用适当的方法:
_rex = re.compile("\d+")if _rex.fullmatch(s): doStuff()
#1
23
\d+
matches any positive number of digits within your string, so it matches the first 78
and succeeds.
\ d +匹配字符串中的任何正数位数,因此它与前78个匹配并成功。
Use ^\d+$
.
Or, even better: "78.46.92.168:8000".isdigit()
或者,甚至更好:“78.46.92.168:8000”.isdigit()
#2
12
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 + $”) 。
#3
7
\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.
\ Z匹配字符串的结尾,而$匹配字符串的结尾或恰好在字符串末尾的换行符之前,并且在re.MULTILINE中表现出不同的行为。有关详细信息,请参阅语法文档。
>>> s="1234\n">>> re.search("^\d+\Z",s)>>> s="1234">>> re.search("^\d+\Z",s)<_sre.SRE_Match object at 0xb762ed40>
#4
5
Change it from \d+
to ^\d+$
将其从\ d +更改为^ \ d + $
#5
5
There are a couple of options in Python to match an entire input with a regex.
Python中有几个选项可以将整个输入与正则表达式进行匹配。
Python 2
In Python 2.x, you may use
在Python 2.x中,您可以使用
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 \n
in 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.search
method requiring the use of ^
/ \A
start-of-string anchor as it does not anchor the match at the start of the string:
或者与上面相同的re.search方法需要使用^ / \一个字符串起始锚点,因为它不会在字符串的开头锚定匹配:
re.search(r'^\d+$')re.search(r'\A\d+\Z')
Note that \A
is an unambiguous string start anchor, its behavior cannot be redefined with any modifiers (re.M
/ re.MULTILINE
can only redefine the ^
and $
behavior).
注意\ A是一个明确的字符串起始锚点,它的行为不能用任何修饰符重新定义(re.M / re.MULTILINE只能重新定义^和$行为)。
Python 3
All those cases described in the Python 2 section and one more useful method, re.fullmatch
(also present in the PyPi regex
module):
所有这些在Python 2部分中描述的情况和一个更有用的方法re.fullmatch(也出现在PyPi正则表达式模块中):
If the whole string matches the regular expression pattern, return a corresponding match object. Return
None
if the string does not match the pattern; note that this is different from a zero-length match.如果整个字符串与正则表达式模式匹配,则返回相应的匹配对象。如果字符串与模式不匹配,则返回None;请注意,这与零长度匹配不同。
So, after you compile the regex, just use the appropriate method:
因此,在编译正则表达式之后,只需使用适当的方法:
_rex = re.compile("\d+")if _rex.fullmatch(s): doStuff()