【发布时间】:2022-01-03 16:31:30
【问题描述】:
假设我有以下文本:
Products to be destroyed: «Prabo», «Palox 2000», «Remadon strong» (Rule). The customers «Dilora» and «Apple» has to be notified.
我需要匹配 «» 引号内的每个字符串,但仅限于以“要销毁的产品:”模式开头或以 (Rule) 模式结尾的时段内。
换句话说,在这个例子中,我不想匹配 Dilora 或 Apple。
获取捕获组中引用内容的正则表达式为:
«(.+?)»
是否可以将其“锚定”到以下模式(例如 Rule)甚至先前的模式(例如“要销毁的产品:”?
这是我在 regex101 上的saved attempt
非常感谢。
【问题讨论】:
-
使用
Products to be destroyed:\s*(«[^«»]*»(?:[\s,]+«[^«»]*»)*)提取然后拆分以获得您需要的块或使用您当前的正则表达式提取引号内的所有块。 -
@WiktorStribiżew 是否需要
(?<=Products to be destroyed: )(«[^«»]*»(?:[\s,]+«[^«»]*»)*)|(«[^«»]*»(?:[\s,]+«[^«»]*»)*)(?= \(Rule)来说明“在以“要销毁的产品:”模式或以(规则)模式结束的时期内。 "