0

Given the following string:

john<str> name2, doe<str>  name1

where str can be string of arbitrary length

I want to select and replace '< str >' and '< str >' with an empty character, such that I get:

john name1, doe name2

I have the following:

new_string = re.sub('<.*>', '', astring, flags=re.DOTALL)

However this is giving me:

john name1
SFD
  • 565
  • 7
  • 18

1 Answers1

0

Make your regex non greedy by adding a ? after the *

astring = 'john<str> name2, doe<str>  name1'
re.sub('<.*?>', '', astring, flags=re.DOTALL)
# 'john name2, doe  name1'
Sunitha
  • 11,777
  • 2
  • 20
  • 23