01Search for a pattern
main.py
import re
text = "Contact: john@example.com"
match = re.search(r"\w+@\w+\.\w+", text)
print(match.group())Output
john@example.com
Python regular expressions with the re module: search, findall, sub, and split to match and transform text patterns.
import re
text = "Contact: john@example.com"
match = re.search(r"\w+@\w+\.\w+", text)
print(match.group())john@example.com
import re
text = "Call 123 or 456 today"
numbers = re.findall(r"\d+", text)
print(numbers)['123', '456']
import re
text = "hello world"
print(re.sub(r"o", "0", text))hell0 w0rld
import re
text = "a1b2c3d"
print(re.split(r"\d", text))['a', 'b', 'c', 'd']