Let's say I want to replace string that start with abc
with replacement
:
import re
s = 'abcdefg'
re.sub(r'^abc', 'replacement', s)
replacementdefg
What should I do to only return replacement
instead of replacementdefg
?
Let's say I want to replace string that start with abc
with replacement
:
import re
s = 'abcdefg'
re.sub(r'^abc', 'replacement', s)
replacementdefg
What should I do to only return replacement
instead of replacementdefg
?
Match the rest of the string with a .*
import re
s = 'abcdefg'
s = re.sub(r'^abc.*', 'replacement', s)
print(s)
output:
replacement