我使用原始字符串表示法来表示一个相当简单的正则表达式,而不是获得匹配对象。空壳公司谈话全文如下:
[~/Documents/Programming/rlm]$ python
python
Python 2.7.5 (default, Aug 25 2013, 00:04:04)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> s = 'bob2323'
s = 'bob2323'
>>> import re
import re
>>> re.match(r'\d+', s)
re.match(r'\d+', s)
>>>
发布于 2013-11-28 11:34:14
您需要使用re.search
。re.match
只尝试匹配从字符串开头开始的字符串。但是,re.search
将搜索整个字符串,寻找与模式匹配的子字符串。
>>> import re
>>> s = "bob2323"
>>> re.match(r'\d+', s)
>>> re.search(r'\d+', s)
<_sre.SRE_Match object at 0x7f4d19beb988>
>>>
有关更多信息,请参见文档中的search() vs. match()
https://stackoverflow.com/questions/20273602
复制相似问题