如何使用Python的regex匹配下划线?

2024-10-06 12:11:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我在使用正则表达式匹配Python中的下划线字符时遇到问题。只是在弹壳里玩,我得到:

>>> import re
>>> re.match(r'a', 'abc')
<_sre.SRE_Match object at 0xb746a368>
>>> re.match(r'_', 'ab_c')
>>> re.match(r'[_]', 'ab_c')
>>> re.match(r'\_', 'ab_c')

我希望至少有一个返回匹配对象。我做错什么了吗?


Tags: 对象importreabobjectmatch字符at
2条回答

请尝试以下操作:

re.search(r'\_', 'ab_c')

你转义下划线字符是对的! 请注意,您只能对字符串的开头使用match,这在文档(https://docs.python.org/2/library/re.html)中也很清楚:

If zero or more characters at the beginning of string match the regular expression pattern, return a corresponding MatchObject instance. Return None if the string does not match the pattern; note that this is different from a zero-length match.

在这种情况下,应该使用search

Scan through string looking for the first location where the regular expression pattern produces a match, and return a corresponding MatchObject instance. Return None if no position in the string matches the pattern; note that this is different from finding a zero-length match at some point in the string.

如果您要查找的模式不在搜索字符串的开头,请使用re.search,而不是re.match

re.match(pattern, string, flags=0)

Try to apply the pattern at the start of the string, returning a match object, or None if no match was found.

re.search(pattern, string, flags=0)

Scan through string looking for a match to the pattern, returning a match object, or None if no match was found.

您不需要转义_,甚至不需要使用原始字符串。

>>> re.search('_', 'ab_c')
Out[4]: <_sre.SRE_Match object; span=(2, 3), match='_'>

相关问题 更多 >