@ Ignacio is right, +1, Iβll just give more examples.
To replace text using a regular expression, use the re.sub function:
sub (pattern, repl, string [, count, flags])
It will replace irrevocable instances of pattern text passed as string . If you need to analyze compliance to extract information about specific group captures, for isntance you can pass a function to the string argument. more details here .
<strong> Examples
>>> import re >>> re.sub(r'a', 'b', 'banana') 'bbnbnb' >>> re.sub(r'/\d+', '/{id}', '/andre/23/abobora/43435') '/andre/{id}/abobora/{id}'
AndrΓ© Pena Jan 03 '17 at 16:02 2017-01-03 16:02
source share