7.33. Regex RE Substitute
re.sub()
Replace matched substring with string
7.33.1. SetUp
>>> import re
7.33.2. Problem
>>> email = 'mwatney@nasa.gov'
>>>
>>> email.replace('@nasa.gov', '@esa.int')
'mwatney@esa.int'
What if there are multiple top-level domains (TLDs)?
>>> email = 'mwatney@nasa.gov'
>>> email = 'mwatney@nasa.com'
>>> email = 'mwatney@nasa.us'
>>> email = 'mwatney@nasa.pl'
String method str.replace()
will fail...
7.33.3. Solution
>>> email = 'mwatney@nasa.gov'
>>>
>>> pattern = r'^(?P<username>[a-z]+)@nasa.[a-z]+$'
>>> replace = r'\g<username>@esa.int'
>>>
>>> re.sub(pattern, replace, email)
'mwatney@esa.int'
7.33.4. Use Case - 1
Usage of re.sub()
:
>>> import re
>>>
>>>
>>> string = 'Baked Beans And Spam'
>>> pattern = r'\s[a-z]{3}\s'
>>> replace = ' & '
>>>
>>> re.sub(pattern, replace, string, flags=re.IGNORECASE)
'Baked Beans & Spam'