match "IF" or "If" not preceded with a dollar sign

91 Views Asked by At

Just can't figure out how I match IF and If but not if or ones prefixed $.

examples:

If True then // <- match 
    
IF True then // <- match 
    
if True then // <- don't match (lowercase i)
     
{SIF NOT DEFINED(SAAS)} // <- don't match (part of another word)
{$IFDEF DEBUG} // <- don't match (preceded with a $)

ChatGPT suggests:

\bIf\b(?!\$)

but it doesn't work when there's a $ before.

1

There are 1 best solutions below

0
cyberbrain On

You don't have to use lookahead or lookbehind here, since you want to match the terms If and IF only as a whole word. Simply use word boundaries:

\bI[Ff]\b