Search code examples
regexnotepad++regex-lookaroundslookbehind

Regex lookbehind is invalid with a vertical bar ( | ), but regex lookahead works just fine in Notepad++


I want to match 子 in these examples in Notepad++

(1) し子 (2) かり子 (3) 子し (4) 子かり

A lookahead such as 子(?=かり|[しつ]) perfectly matches it at line (3) and (4), with no problem. But a lookbehind such as (?<=かり|[しつ])子 for some reason is invalid and can't match it at line (1) and (2) at all, even though individually (?<=かり)子 and (?<=[しつ])子 work just fine. Why's that?


Solution

  • Your problem is that your negative lookbehind has variable length (either 2 characters かり or one character [しつ], and variable length lookbehinds are not supported by most regex engines, including the one in Notepad++. If you can remove the first character from the lookbehind () i.e.

    (?<=り|[しつ])子
    

    or more optimally

    (?<=[りしつ])子
    

    It will work, although it will match preceded by and any character. If that is not acceptable, you could use a capturing group instead i.e.

    (かり|[しつ])子
    

    and include $1 in your replacement