Search code examples
regexregex-lookarounds

Positive lookbehind after negative lookbehind


Regex: (?<!xx)(?<=34)56(?=78) with global flag

12345678

xx345678

Why does it match 56 on both lines? Shouldn't (?<!xx) exclude second line?


Solution

  • You can use this regex with a nested negative lookbehind:

    (?<=(?<!xx)34)56(?=78)
    

    RegEx Demo

    (?<!xx) inside the (?<=(?<!xx)34) will assert failure if xx is positioned just before 34.