源字符串是:
# Python 3.4.3
s = r'abc123d, hello 3.1415926, this is my book'
Run Code Online (Sandbox Code Playgroud)
这是我的模式:
pattern = r'-?[0-9]+(\\.[0-9]*)?|-?\\.[0-9]+'
Run Code Online (Sandbox Code Playgroud)
但是,re.search可以给我正确的结果:
m = re.search(pattern, s)
print(m) # output: <_sre.SRE_Match object; span=(3, 6), match='123'>
Run Code Online (Sandbox Code Playgroud)
re.findall 只是转出一个空列表:
L = re.findall(pattern, s)
print(L) # output: ['', '', '']
Run Code Online (Sandbox Code Playgroud)
为什么不能re.findall给我预期的清单:
['123', '3.1415926']
Run Code Online (Sandbox Code Playgroud) 我试图在URL字符串上使用python正则表达式.
id= 'edu.vt.lib.scholar:http/ejournals/VALib/v48_n4/newsome.html'
>>> re.search('news|ejournals|theses',id).group()
'ejournals'
>>> re.findall('news|ejournals|theses',id)
['ejournals', 'news']
Run Code Online (Sandbox Code Playgroud)
根据http://docs.python.org/2/library/re.html#finding-all-adverbs上的文档,它说search()匹配第一个,并查找字符串中所有可能的匹配.
我想知道为什么'新闻'没有被搜索捕获,即使它在模式中被宣布为第一.
我使用了错误的模式吗?我想搜索字符串中是否出现任何关键字.