REGEX To Find Acronyms Outside of Parenthesis

67 Views Asked by At

I'm trying to find all instance of an acronym outside enclosing parenthesis; e.g., I'm looking for FO, but not (FO). While the term can be defined as an acronym; e.g., Faint Objects (FO), the acronym is only used in specific instances (FO I/FO II). Except for these instances, the acronym needs to be spelled out in the document.

To complicate it, I also need to find instances where the acronym is used in a parenthetical phrase; e.g., (FO are hard to see) and (like non-FO). In these instances, the acronym also needs to be spelled out.

I tried:

(?<!\x28)HQ(?!\x29)

Which works well except for (FO are hard to see); i.e., acronym preceded by parenleft.

1

There are 1 best solutions below

7
sln On

Usually, it's rendered un-ambigous like this :

FO(?<!\(FO(?=\)))

https://regex101.com/r/Rkz9oN/1

 FO
 (?<!
    \( FO
    (?= \) )
 )

This uses a compound nested assertion.

Note that: If using the new JS engine with variable length look behind
the FO can be variable and used as a back reference :

(FO)(?<!\(\1(?=\)))

https://regex101.com/r/jxFSmX/1

This facilitates finding multiple Acronyms in a single regex guarded by this assertion :

(FO|FQ|FRY|HW)(?<!\(\1(?=\)))

https://regex101.com/r/OCEmNy/1

In-fact you can have every single Acronym you can type or cut'n paste) put into a trie regex generator (https://regexformat.com/scrn8/TernSort.jpg) making it a formidable tool.