【问题标题】:Find text fragment with Regex A that can't be overlapped by regex B用正则表达式 A 查找不能被正则表达式 B 重叠的文本片段
【发布时间】:2022-10-14 05:18:13
【问题描述】:

我继续学习先进的 PCRE 技术。 请帮我为以下任务制作一个正则表达式(或告知不可能):

对于任何模式 A 和 B,找到一个与 A 相对应且不能与 B 重叠的文本片段。

所需的正则表达式应插入底层表达式而不是 <...>

<...>(?(DEFINE)(?'A'<pattern A>)(?'B'<pattern B>))

这应该是this request 的一些通用案例,几乎可以使用任何模式 A 和 B。

我尝试了不同的变体但失败了。

【问题讨论】:

  • 我觉得你会问越来越多的问题,直到我们一起达到 PCRE 能力的天花板:) 这个问题很有趣。当我有更多时间时,如果有人不早点做,我会考虑解决方案

标签: regex pcre


【解决方案1】:

我们可以想象这样的事情:

^(?*(.*?)(((?&A))(.*+)))(?!  # Try all possible variants of A
   .*?(?(?=.*?$)
      (?&B)(?!.*?$)  # B started before the beginning of A or together with the beginning of A, and ended after the beginning of A
      |
      (?=.+?$)(?&B)  # B started after the beginning of A, but before the end of A
))K(?(DEFINE)(?<A>w+)(?<B>d))

Demo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-22
    • 1970-01-01
    • 2014-08-11
    • 2017-09-19
    相关资源
    最近更新 更多