regexlookbehindnegative-lookbehind

How to not match (negative match) certain patterns in lookbehind


I'd like to match the word Date only in these strings:

Not in these strings:

I've managed everything except for not match the Date in Java.localDate; with:

(?<=\w)Date(?=;|\()

Please check my Regex101 demo.

Could someone please help with the last step? Thanks!


Solution

  • The regex demo you've given, selected "PCR2 (PHP >= 7.3)" as regex engine, so in that case you could use the \K (i.e., "keep out") feature:

    (?<!\.)\b\w+\KDate(?=[;(]) (regex demo)

    This rejects inputs where the word ending in "Date" is preceded by a point, like in Java.localDate;. If there are other characters (punctuation?) which also cannot occur right before the word, then list them like this (?<![.,;]).

    If you don't have support for \K in your regex engine, then you could use a capture group. You'd match the whole word, but capture group 1 will represent "Date" in the match:

    (?<!\.)\b\w+(Date)(?=[;(]) (regex demo)