【发布时间】:2020-12-02 08:47:54
【问题描述】:
我有如下文字-
02052020 02:40:02.445: Vacation Allowance: 21; nnnnnn Vacation Allowance: 22;nnn
我想在 Python 中提取以下内容-
Vacation Allowance: 21
Vacation Allowance: 22
基本上,我想提取所有出现的“Vacation Allowance:”以及后缀为;的数值
我正在使用下面的正则表达式-
(.*)(Vacation Allowance:)(.*);(.*)
下面是完整的 Python 代码-
import re
text = '02/05/2020 Vacation Allowance: 21; 02/05/2020 Vacation Allowance: 22; nnn'
pattern = re.compile(r'(.*)(Vacation Allowance:)(.*);(.*)')
for (a,b,c,d) in re.findall(pattern, text):
print(b, " ", c)
这并不是所有的出现,而是只给出最后一次出现。当前输出是-
Vacation Allowance: 22
您能否评论一下我如何提取所有匹配项?
【问题讨论】:
-
只要使用
\bVacation Allowance:\s*\d+; -
你想用什么语言实现这个?
-
您使用什么语言/工具?来自regex tag info:“由于正则表达式尚未完全标准化,所有带有此标签的问题还应包含一个指定适用的编程语言或工具的标签。”
-
我在 Python 中需要这个。
-
@anubhava- 感谢您的评论。我试过你的建议,但它不起作用。顺便说一句,我正在 Python 中尝试这个。我现在已经在原来的问题中澄清了这一点。
标签: python regex regex-group