Re.sub not working for me

You are assigning the result of re.sub back to a variable, right? e.g.

lines = re.sub(pattern, key[1], lines)

It’s a string, so it can’t be changed (strings are immutable in Python), therefore a new string is created and returned to you. If you don’t assign it back to a name, you will lose it.

Leave a Comment