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
Post a Comment