I have a bunch of strings like this:
abc#axyz(abc#axyz#a#aabc)abc#axyz. What I need to do is remove all the #a that appear in the text between brackets, while those outside should remain. I’ve tried the following:
\((.*?)(#a)(.*?)\)
But it only catches the first repetition. What am I getting wrong? Thanks for any input!
>Solution :
Try (regex101 link):
import re
s = "abc#axyz(abc#axyz#a#aabc)abc#axyz"
out = re.sub(r"#a(?=.*\))(?!.*\()", "", s)
print(out)
Prints:
abc#axyz(abcxyzabc)abc#axyz