I need to exclude a string from being matched if it's preceeded by a certain character, and my regex engine is POSIX. I was able to get the desired result using a negative lookbehind on https://regexr.com/ but just discovered that won't work on my POSIX SnowFlake platform :-( .
I'm trying to standardize variations of company names and want to match the strings that end in 'COMPANY', 'CO', or 'CO.', but not match them if preceeded by an ' & '. So 'COMPANY' would get matched in 'POWERWASH COMPANY', but not in 'JONES & COMPANY'.
Is there a way I can accomplish this in POSIX regex? I was able to get this to work using a negative lookbehind as follows:
(?<!&)( COMPANY$| CO[.]?$)
You can use
(^|[^&])( COMPANY| CO[.]?)$
See the regex demo.
Whatever you capture is usually of no importance in POSIX regex, but in other cases it is usually easy to work around using additional capturing groups and code logic.
Regex details:
(^|[^&])
- start of string or any char other than &
( COMPANY| CO[.]?)
- either a space and COMPANY
, or a space, CO
, an optional .
and$
- end of string