Regex to replace matches but also ignore when matches within quotes

Viewed 64

Trying to match and replace "and" or "or" to "&" and "|" when it occurs outside of quotes except when they occur within quotes.

Quotes could be single(') or double(").

The string is as follows:

Industry ='Education' or Industry =\"Energy\" or Industry = 'Financial or Bank' or Industry = 'Hospitality' or Industry = \"Food and Beverage\"  and Industry = 'Utilities'

Expected output:

Industry ='Education' | Industry =\"Energy\" | Industry = 'Financial or Bank' | Industry = 'Hospitality' | Industry = \"Food and Beverage\"  & Industry = 'Utilities'

I know that we might have to use lookarounds but cant figure out how. I am using stringr package in R for all my regex manipulations.

Let me know if you need more info.

3 Answers
Related