源字符串是:
# Python 3.4.3
s = r'abc123d, hello 3.1415926, this is my book'
这是我的模式:
pattern = r'-?[0-9]+(\\.[0-9]*)?|-?\\.[0-9]+'
但是,re.search
可以给我正确的结果:
m = re.search(pattern, s)
print(m) # output: <_sre.SRE_Match object; span=(3, 6), match='123'>
re.findall
只是转储一个空列表:
L = re.findall(pattern, s)
print(L) # output: ['', '', '']
为什么不能re.findall
给我预期的列表:
['123', '3.1415926']
最佳答案
这里有两点需要注意:
re.findall
如果正则表达式模式包含捕获组,则返回捕获的文本- 模式中的
r'\\.'
部分匹配两个连续的字符、\
以及除换行符之外的任何字符。
If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group. Empty matches are included in the result unless they touch the beginning of another match.
请注意,要使 re.findall
仅返回匹配值,您通常可以
- 删除冗余捕获组(例如
(a(b)c)
->abc
) - 将所有捕获组转换为 non-capturing (也就是说,将
(
替换为(?:
) 除非存在引用模式中的组值的反向引用(然后参见下文) - 使用
re.finditer
代替([x.group() for x in re.finditer(pattern, s)]
)
在您的情况下,findall
返回了所有捕获的空文本,因为您在 r''
字符串文字中有 \\
试图匹配文字 \
。
要匹配数字,您需要使用
-?\d*\.?\d+
正则表达式匹配:
-?
- 可选减号\d*
- 可选数字\.?
- 可选的小数点分隔符\d+
- 1 位或更多数字。
参见demo
这里是IDEONE demo :
import re
s = r'abc123d, hello 3.1415926, this is my book'
pattern = r'-?\d*\.?\d+'
L = re.findall(pattern, s)
print(L)
关于python - re.findall 表现得很奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55541803/