关于zerowidth断言不支持不确定的长期问题

2024-10-03 06:24:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用python re模块的正则表达式来标识此模式下字符串中的4个数字:

The number of qualified individuals is 13553, company is 2500, The actual number of individuals joined is 7187,compny is 1722.

当我使用零宽度断言来标识company is之后的数字时,我被卡住了。我不知道如何识别最后一个数字,因为有两个相同的company is(?<="some words")表达式不支持无限长

import re
content = "The number of qualified individuals is 13553, company is 2500, The actual number of individuals joined is 7187,company is 1722."

match_first = re.search("The actual.*\d\\b", content).group()
print(match_first)

match_content = re.search("(?<=company\sis\s)+\d+", match_first).group()
print(match_content)

事实上,这种方法也可以进行匹配,但它很麻烦,我总是觉得有一种方法可以将最后一个数字与单个语句进行匹配。它只匹配数字,其他的不匹配,可能是因为我学习了正则表达式。时间太短了,我花了一个晚上研究正则表达式,但还是没有找到方法。根据昨晚的理解,可能需要使用递归匹配来解决它。我还需要一些时间来完成它,但我想一定有办法


Tags: ofthe方法renumberismatch数字