【问题标题】:How to Retrieve Overlapping Matches with Complex Regex and Preg_Match_All in PHP如何在 PHP 中使用复杂的 Regex 和 Preg_Match_All 检索重叠匹配
【发布时间】:2019-10-29 01:31:38
【问题描述】:

已阅读以下内容,这些内容与我面临的问题有一些重叠(双关语!): preg_match_all how to get *all* combinations? Even overlapping ones Overlapping matches with preg_match_all and pattern ending with repeated character

但是,我真的不知道如何将他们的答案应用到我的问题上,这有点复杂。

我与 preg_match_all() 一起使用的正则表达式:

/.{240}[^\[]Order[^ ][^\(].{9}/u

使用以下字符串:

56A.  Subject to the provisions of this Act, any decision of the Court or the Appeal Board shall be final and conclusive, and no decision or order of the Court or the Appeal Board shall be challenged, appealed against, reviewed, quashed or called into question in any court and shall not be subject to any Quashing Order, Prohibiting Order, Mandatory Order or injunction in any court on any account.[20/99; 42/2005]

我打算让它精确匹配 3 次。第一场比赛在结束前有 9 个字符的“取消令”。第二场比赛在结束前有 9 个字符的“禁止令”。第三场比赛在结束前有 9 个字符的“强制顺序”。

但是,正如预期的那样,它只匹配第一个,因为预期的匹配是重叠的。

我应用了我在其他帖子中读到的内容,我尝试了这个:

(?=(.{240}[^\[]Order[^ ][^\(].{9}))

我仍然没有得到我需要的东西。

我该如何解决这个问题?

【问题讨论】:

标签: php regex preg-match-all


【解决方案1】:

你可以使用

\w+\s+Order\b

请参阅regex demo

正则表达式详细信息

  • \w+ - 一个或多个单词字符
  • \s+ - 1 个或多个空格
  • Order\b - 一个完整的单词Order,因为\b 是一个单词边界。

【讨论】:

    【解决方案2】:

    您需要对.{240} 使用积极的后向断言,就像您找到的答案建议对.{9} 使用积极的前瞻断言一样:

    /(?<=.{240})[^\[]Order[^ ][^\(](?=.{9})/u
    

    正如@bobblebubble 所说,由于[^ ],此RE 仅匹配您的字符串两次。根据需要调整该部分。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-28
      • 2017-04-04
      • 2011-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多