1

Given a string s as follows, I want to remove substring between but and ball multiple times:

s = 'I like sport, but I don\'t like football; I like sport, but I don\'t like basketball'
re.sub('but.*ball', '', s, flags=re.MULTILINE)

Out:

'I like sport, '

How could I get the expected result like this:

'I like sport, I like sport'
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
ah bon
  • 9,293
  • 12
  • 65
  • 148

1 Answers1

0

Try adding a question mark:

>>> re.sub('but.*?ball|[,;]', '', s, flags=re.MULTILINE).strip()
'I like sport  I like sport'
>>> 
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
  • "If you give a man a fish, you feed him for a day. If you teach a man to fish, you feed him for a lifetime." – PatrickT Apr 11 '23 at 06:26