0

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'
U12-Forward
  • 65,118
  • 12
  • 70
  • 89
ah bon
  • 7,903
  • 7
  • 43
  • 86

1 Answers1

0

Try adding a question mark:

>>> re.sub('but.*?ball|[,;]', '', s, flags=re.MULTILINE).strip()
'I like sport  I like sport'
>>> 
U12-Forward
  • 65,118
  • 12
  • 70
  • 89