I have a variable with a multiline string and I'm trying to replace the line test v1.0 with upside but the problem is I'm trying to replace it only if the entire line matches the pattern so it shouldn't replace the first test v1.0.1
pkgLogExtract = dedent("""
test v1.0.1
nothing
test v1.0
out
in
flip
""")
print (re.sub(r'\^test v1.0\$', "upside", pkgLogExtract, 1))
I tried using re.sub and putting '\^test v1.0\$' as the pattern to replace but it doesn't replace anything. I've also tried it with the raw flag, so r'\^test v1.0\$' but that doesn't replace anything either. Any idea what I could do?
\^matches a literal^. Same goes about$. You need to passflags=re.Mto there.suband remove the backslashes from^and$. And escape the.. See ideone.com/ijPIrayour_string.replace("test v1.0\n", "upside")too brittle?replacewould work in every situation so I opted for regex (the above script is just pseudo code)