Search code examples
regexpcre

Find text fragment with Regex A that can't be overlapped by regex B


I continue to learn advanced PCRE techniques. Please help me make a regex (or inform about impossibility) for the following task:

For any patterns A and B, find a text fragment that would correspond to A, and could not be overlapped by B.

The desired regex should be inserted into underlying expression instead the <...>

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

This should be some generalized case of this request that will be able to work with almost any patterns A and B.

I try different variants but failed.


Solution

  • We can imagine something like this:

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

    Demo