python - re.findall 表现得很奇怪

标签 python regex

源字符串是:

# 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'\\.' 部分匹配两个连续的字符、\ 以及除换行符之外的任何字符。

参见findall reference :

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/

相关文章:

java - 匹配一个 ant 正则表达式,但不匹配其他正则表达式

php - 如何检查字符串在 4 个连续字符后是否没有元音

python - 如何使用可变长度分隔符分割文本

python - 尝试导入与内置模块同名的模块会导致导入错误

c# - 是否可以使用 String.Split 或正则表达式将字符串拆分为字符串数组并删除不在分隔符之间的部分?

python - 如何以编程方式从 Python 提升到 Visual Studio 的顶部

python - 为什么 selenium webdriver 在每次函数调用时都会访问网络?

css - 在时尚中排除网站的子文件夹?

使用管理员运行的 Python Windows 任务

python如何获取BytesIO分配的内存长度?