Search code examples
regexpositive-lookahead

REGEX: Select KeyWord1 if KeyWord2 is in the same string


I am trying to capture KEYWORD1 in .NET regex engine based on whether KeyWord2 is present in the string. So far the positive look-around solution I am using:

(?=.*KeyWord2)**KEYWORD1** (\m\i)

RegEx Test Link

only captures KEYWORD1 if KeyWord2 is positioned anywhere behind KEYWORD1 in the string. How can I optimize this in regex so that it captures all instances of KEYWORD1 in the string despite the position of KeyWord2 being ahead, behind or both?

I'd really appreciate some insight.

Thank You


Solution

  • You can use the regex below for your requirement:

    \bKEYWORD1\b(?:(?<=\bKeyWord2\b.*?)|(?=.*?\bKeyWord2\b))
    

    Explanation of the above Regular Expression:

    gi - Use the flags(in order to avoid any case difference) representing: g - global; i - case-insensitive

    \b - Represents a word boundary.

    (?:) - Represents a non-capturing group.

    (?=.*?KeyWord2) - Represents the positive lookahead which matches all KEYWORD1 which are before KeyWord2 read from left to right.

    | - Represents alternation; that is it alternates between 1st and 2nd alternating group.(Although, you can wrap them in group.)

    (?<=KeyWord2.*?) - Represents infinite(because non-fixed width lazy identifier .*? used) positive lookbehind which matches all KEYWORD1 which are behind of KeyWord2.

    You can find the above regex demo here.

    NOTE - For the record, these engines support infinite lookbehind:

    As far as I know, they are the only ones.