-1

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?

CHRD
  • 1,917
  • 1
  • 15
  • 38

1 Answers1

1

Match the rest of the string with a .*

import re

s = 'abcdefg'

s = re.sub(r'^abc.*', 'replacement', s)
print(s)

output:

replacement
rdas
  • 20,604
  • 6
  • 33
  • 46