python - 需要正则表达式的帮助来获取强制值之后的任何内容

标签 python regex python-2.7 regex-lookarounds

我有一个文本,需要抓取数据并将其拆分。我需要在一大组文本中找到“审阅频率”,然后一旦找到,就将其后面的所有内容放在“)”处。
示例文本为:

No. of components Variable
Review frequency Quarterly (Mar., Jun., Sep., Dec.)
Quick facts
To learn more about the

我需要的是“季度”和“三月、六月、九月、十二月”

我当前的正则表达式是:

((?=.*?\bReview frequency\b)(\b(Q|q)uarterly|(A|a)nnually|(S|s)emi-(A|a)nnually))

但这不起作用。本质上,在我们开始获取其他信息之前,“审阅频率”需要成为限定符,因为文件中可能还有其他日期/期间。谢谢!

最佳答案

您与该行的其余数据不匹配。

我建议使用:

(?m)^Review frequency[ \t]+(\w+)[ \t]+(.+)

请参阅regex demo

如果第一个捕获组只能包含模式中所示的 3 个单词,请使用

(?m)^Review frequency[ \t]+([Qq]uarterly|(?:[Ss]emi-)?[Aa]nnually)[ \t]+(.*)

参见another regex demo

Use these patterns with re.findall :

import re
regex = r"(?m)^Review frequency[ \t]+([Qq]uarterly|(?:[Ss]emi-)?[Aa]nnually)[ \t]+(.*)"
test = "No. of components Variable\nReview frequency Quarterly (Mar., Jun., Sep., Dec.\nQuick facts\nTo learn more about the"
print(re.findall(regex, test))

关于python - 需要正则表达式的帮助来获取强制值之后的任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40158149/

相关文章:

python - 如何在Python中对多个术语使用正向和负向前瞻?

python - ._GLOBAL_DEFAULT_TIMEOUT 发生在简单的 urlopen 上

python - 如何从 Python re 获取不匹配的正则表达式组?

python - 如何更直接地引用函数?

python - 在Python中的不同条件下匹配具有相同名称的子组

sql - 如何为字符串列表中的每个元素添加引号

python - 为什么属性包装的成员列表显示意外的值?

python - 如何阻止 Matplotlib 导航工具栏缩放在绘图更新时重置?

python - 计算另一列是否包含值

python - 模块未找到错误: No module named 'keras' in AI DevCloud Intel