首页 > 解决方案 > 关于零宽度断言不支持不确定的长期问题

问题描述

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

合格人数13553人,公司2500人,实际入会人数7187人,公司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)

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

标签: pythonpython-3.x

解决方案


推荐阅读