在 RegEx 中需要帮助以获取强制值后的任何内容
Need help in RegEx to grab anything after a mandatory value
我有一个文本,我需要在其中获取数据并将其拆分。我需要在一大组文本中找到 "Review frequency",然后一旦找到,就把它后面的所有内容都拿走并停在 ')' 处。
示例文本是:
No. of components Variable
Review frequency Quarterly (Mar., Jun., Sep., Dec.)
Quick facts
To learn more about the
我需要的是'Quarterly'和'Mar., Jun., Sep., Dec.'
我当前的正则表达式是:
((?=.*?\bReview frequency\b)(\b(Q|q)uarterly|(A|a)nnually|(S|s)emi-(A|a)nnually))
但这不起作用。本质上,在我们开始获取其他信息之前,'Review frequency' 需要作为限定符,因为文件中可能还有其他 dates/periods。谢谢!
您没有匹配该行的其余数据。
我建议使用:
(?m)^Review frequency[ \t]+(\w+)[ \t]+(.+)
如果第一个捕获组只能包含模式中指示的 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))
我有一个文本,我需要在其中获取数据并将其拆分。我需要在一大组文本中找到 "Review frequency",然后一旦找到,就把它后面的所有内容都拿走并停在 ')' 处。
示例文本是:
No. of components Variable
Review frequency Quarterly (Mar., Jun., Sep., Dec.)
Quick facts
To learn more about the
我需要的是'Quarterly'和'Mar., Jun., Sep., Dec.'
我当前的正则表达式是:
((?=.*?\bReview frequency\b)(\b(Q|q)uarterly|(A|a)nnually|(S|s)emi-(A|a)nnually))
但这不起作用。本质上,在我们开始获取其他信息之前,'Review frequency' 需要作为限定符,因为文件中可能还有其他 dates/periods。谢谢!
您没有匹配该行的其余数据。
我建议使用:
(?m)^Review frequency[ \t]+(\w+)[ \t]+(.+)
如果第一个捕获组只能包含模式中指示的 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))