python - How to replace part of str using `re.sub()`? -


i can replace oldtext newtext1 re.sub("'(.*)'", 'newtext', "test = 'oldtext'") trying use re.sub() re.sub("'test = (.*)'", 'newtext', "test = 'oldtext'")

can pass actual text in pattern?

i think numbers bad example updated str. first 1 works second 1 doesn't. isn't allowed have actual text in pattern?

>>> import re >>>  >>> re.sub(r"(test = )'.*'", r"\1'" + str(22) + r"'", "test = '21'") "test = '22'" 

you can need grab other parts of match , piece new result.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -