Here's how it could be done:
import re
def replace_FOO(m):
if m.group(1) is None:
return m.group()
return m.group().replace("FOO", "XXX")
string = "' FOO ' abc 123 ' def FOO ghi 345 ' FOO '' FOO ' lmno 678 FOO '"
output = re.sub(r"'[^']*'|([^']*)", replace_FOO, string)
print(string)
print(output)
[EDIT]
The re.sub
function will accept as a replacement either a string template or a function. If the replacement is a function, every time it finds a match it'll call the function, passing the match object, and then use the returned value (which must be a string) as the replacement string.
As for the pattern itself, as it searches, if there's a '
at the current position it'll match up to and including the next '
, otherwise it'll match up to but excluding the next '
or the end of the string.
The replacement function will be called on each match and return the appropriate result.
Actually, now I think about it, I don't need to use a group at all. I could do this instead:
def replace_FOO(m):
if m.group().startswith("'"):
return m.group().replace("FOO", "XXX")
return m.group()
string = "' FOO ' abc 123 ' def FOO ghi 345 ' FOO '' FOO ' lmno 678 FOO '"
output = re.sub(r"'[^']*'|[^']+", replace_FOO, string)