我有一个文本,需要抓取数据并将其拆分。我需要在一大组文本中找到“审阅频率”,然后一旦找到,就将其后面的所有内容放在“)”处。
示例文本为:
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]+(.*)
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/