8.19. RE Substitute

  • re.sub()

  • Replace matched substring with text

8.19.1. Examples

Usage of re.sub():

>>> import re
>>>
>>>
>>> DATA = 'Baked Beans And Spam'
>>> pattern = r'\s[a-z]{3}\s'
>>>
>>> re.sub(pattern, ' & ', DATA, flags=re.IGNORECASE)
'Baked Beans & Spam'