Scripting >> Python >> Examples >> Regex >> Examples of regex using re module

 

Spliting strings using multiple separators >>> import re
>>> re.split(r"[.?!]", "One sentence. Another one? And the last one!")
['One sentence', ' Another one', ' And the last one', '']
Spliting strings using multiple separators and capturing the separators >>> import re
>>> re.split(r"([.?!])", "One sentence. Another one? And the last one!")
['One sentence', '.', ' Another one', '?', ' And the last one', '!', '']
Searching for specific string to mask it >>> import re
>>> re.sub(r"[\w.%+-]+@[\w.-]+", "[REDACTED]","Received an email for go_nuts95@myexample.com")
'Received an email for [REDACTED]'
Swaping parts of a string >>> import re
>>> re.sub(r"^([\w .-]*), (\w .-]*)$", r" ","Lovelace, Ada")
'Lovelace, Ada'