Program 1
Find all numbers
Extract digits from a string.
python
import re
text = "Order 42 ships on day 7 with code 100x"
nums = re.findall(r"\d+", text)
print(nums)Topic 17 of 17
Pattern-match text with Python's re module.
Program 1
Extract digits from a string.
import re
text = "Order 42 ships on day 7 with code 100x"
nums = re.findall(r"\d+", text)
print(nums)Program 1
A simple email pattern check.
import re
pattern = r"^[w.-]+@[w-]+.[a-zA-Z]{2,}$"
for e in ["a@b.com", "bad@", "ok.user@mail.co"]:
print(e, bool(re.match(pattern, e)))Program 2
Collapse multiple spaces into one.
import re
s = "this has too many spaces"
clean = re.sub(r"s+", " ", s)
print(clean)Program 1
Parse a date with named groups.
import re
m = re.match(r"(?P<y>d{4})-(?P<m>d{2})-(?P<d>d{2})", "2026-06-05")
if m:
print(m.group("y"), m.group("m"), m.group("d"))