【问题标题】:Need help in RegEx to grab anything after a mandatory value在 RegEx 中需要帮助以获取强制值后的任何内容
【发布时间】:2016-10-20 15:05:55
【问题描述】:

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

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

我需要的是“季刊”和“3 月、6 月、9 月、12 月”

我目前的正则表达式是:

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

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

【问题讨论】:

标签: python regex python-2.7 regex-lookarounds


【解决方案1】:

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

我建议使用:

(?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))

【讨论】:

  • 非常感谢第二个工作!如果我需要将 re.MULTILINE 合并到这个中怎么办?还是单独打印捕获组?
  • re.findall 结果分配给变量后,您可以根据需要打印结果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-31
  • 1970-01-01
  • 2023-04-02
  • 2018-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多