I have a multiline string with three of the following lines of the following form:
Text1 Text2a Text3
Text1 Text2b Text3
Text1 Text2! Text3
I wish to replace all texts between Text1 and Text3 with Text4, unless the intermediate text contains the character !. Thus, the desired output is:
Text1 Text4 Text3
Text1 Text4 Text3
Text1 Text2! Text3
Let c be the multiline string above. I believe re.sub is the natural choice for this problem, so I tried the following:
c = re.sub("Text1(.*?)(?!=\!)Text3", "Text1 Text4 Text3", c, flags=re.DOTALL)
However, it replaces every intermediate text with Text4. That is, I get the following output:
Text1 Text4 Text3
Text1 Text4 Text3
Text1 Text4 Text3
How can I resolve this?
(.*?), matches all characters including !, before Text3, thus the negative lookahead won't have any remaining characters left to prevent the match."(?<=Text1)([^!]*?)(?=Text3)"g, look around for our boundaries and capture everything between that not includes!. regex101.com/r/o97cBR/1