Regex for finding a phrase that isn't already updated

49 Views Asked by At

I'm looking for some assistance with Regex. I have a project in which I'm writing a VBA macro to update hundreds of thousands of documents by find/replacing a phrase. The Phrase is "Company Name" and needs to be "Company Name LLP". The issue is that with Find/Replace I cannot prevent "Company Name LLP" from becoming "Company Name LLP LLP" (two LLPs) if it was previously updated. My workaround is to find "LLP LLP" and replace with "LLP" however there's obviously a better way using Regex. I'd like a regex that finds all "Company Name" WHERE NOT already "Company Name LLP" (even better it would also exclude "Company Name, LLP" with a comma). Can anyone please assist me with building this Regex? TIA!

1

There are 1 best solutions below

3
On BEST ANSWER

You may use a (?!\s+LLP) negative lookahead to avoid matching Company Name before 1+ whitespaces and an LLC word:

Company Name(?!\s+LLP)

If these are whole words, enclose with word boundaries:

\bCompany Name\b(?!\s+LLP\b)

To make the expression case insensitive, enable the case insensitive mode on the RegExp object.

Example:

Set myRegExp = New RegExp
myRegExp.IgnoreCase = True 'Here, case insensitive mode is ON
myRegExp.Global = True
myRegExp.Pattern = "\bCompany Name\b(?!\s+LLP\b)"