I'm looking to do replacements in unknown third-party inputs in strings that sometimes have quotes among them.
I want to replace a wholeword whereever it occurs unless it's in double or single-quotes, and unless the quote is escaped.
Example: Replacing FOO by BAR
Input:
FOO "FOO" 'FOO' " 1 + FOO + 2 " ABCFOOXYZ " str1\"FOO\"str3'FOO'\'\'" ' str1\'FOOstr3"FOO"\"\"' \"FOO\"
Expected output:
BAR "FOO" 'FOO' " 1 + FOO + 2 " ABCFOOXYZ " str1\"FOO\"str3'FOO'\'\'" ' str1\'FOOstr3"FOO"\"\"' \"BAR\"
More tests:
name: FOO
favoriteQuote: "I am my own FOO."
children: 'FOO\'s children'
cars: ownersList[FOO]
statement = FOO + " is the owner of the house."
Expected output:
name: BAR
favoriteQuote: "I am my own FOO."
children: 'FOO\'s children'
cars: ownersList[BAR]
statement = BAR + " is the owner of the house."
I saw this question: Match and replace a word not in quotes (string contains escaped quotes) which I thought was similar and could be a good starting point the accepted answer does not work at all:
https://regex101.com/r/Lfan64/5
If anyone could help me get the expected result from my regex that would be great, thanks.