Regex
Python - A Quick Start for existing Programers
1 min read
This section is 1 min read, full guide is 44 min read
Published Sep 16 2025, updated Sep 30 2025
21
Show sections list
0
Log in to enable the "Like" button
0
Guide comments
0
Log in to enable the "Save" button
Respond to this guide
Guide Sections
Guide Comments
Python
Python implements Regex using the re module, imported like this:
import re
Copy to Clipboard
The key functions are:
re.search(pattern, string)→ Finds the first match anywhere in the string.re.match(pattern, string)→ Matches only at the beginning of the string.re.fullmatch(pattern, string)→ The entire string must match.re.findall(pattern, string)→ Returns all matches as a list.re.finditer(pattern, string)→ Returns an iterator of match objects.re.sub(pattern, repl, string)→ Replaces matches withrepl.re.split(pattern, string)→ Splits string by the pattern.re.compile(pattern)→ Pre-compiles a regex for reuse.
Examples:
import re
text = "Python 3.12 is released in 2023!"
# Search
match = re.search(r"\d+\.\d+", text)
print("Search:", match.group() if match else None)
# -> "3.12"
# Match (start only)
print("Match:", re.match(r"Python", text))
# -> Match object (at start)
# Fullmatch
print("Fullmatch:", re.fullmatch(r".+", text) is not None)
# -> True (entire string matches ".+")
# Findall
print("Findall digits:", re.findall(r"\d+", text))
# -> ['3', '12', '2023']
# Finditer
for m in re.finditer(r"\d+", text):
print("Finditer:", m.group(), "at", m.span())
# -> 3 at (7, 8)
# -> 12 at (8, 10)
# -> 2023 at (24, 28)
# Substitution
print("Sub:", re.sub(r"\d+", "#", text))
# -> "Python #.# is released in #!"
# Split
print("Split:", re.split(r"\s+", text))
# -> ['Python', '3.12', 'is', 'released', 'in', '2023!']
# Compile for reuse
pattern = re.compile(r"\d+")
print("Compile Findall:", pattern.findall(text))
# -> ['3', '12', '2023']
# Example text
text = '''
Emails: alice@example.com, bob.smith99@test.co.uk, not-an-email@bad, "quoted string"
Passwords: Passw0rd!, weakpass, Strong#123, NoSpecialChar1
Quotes: She said "Hello World", then "Regex is fun!" and finally "1234"
'''
# Find all email addresses
emails = re.findall(r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}", text)
print("Emails:", emails)
# -> ['alice@example.com', 'bob.smith99@test.co.uk']
# Extract everything inside double quotes
quotes = re.findall(r'"(.*?)"', text)
print("Quoted strings:", quotes)
# -> ['quoted string', 'Hello World', 'Regex is fun!', '1234']
# Validate strong password
# Must contain: ≥8 chars, 1 uppercase, 1 lowercase, 1 digit, 1 special char
password_pattern = re.compile(
r'^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$'
)
test_passwords = ["Passw0rd!", "weakpass", "Strong#123", "NoSpecialChar1"]
for pwd in test_passwords:
print(f"{pwd:15} ->", bool(password_pattern.fullmatch(pwd)))
# -> Passw0rd! -> True
# -> weakpass -> False
# -> Strong#123 -> True
# -> NoSpecialChar1 -> False
# Find all numbers (integer or decimal)
numbers = re.findall(r"\d+(?:\.\d+)?", text)
print("Numbers:", numbers)
# -> ['99', '0', '1234']
# Extract all words starting with capital letters
capitals = re.findall(r"\b[A-Z][a-zA-Z]+\b", text)
print("Capitalized words:", capitals)
# -> ['Emails', 'Passwords', 'Quotes', 'She', 'Hello', 'World', 'Regex']
# Split text into sentences (simple case)
sentences = re.split(r"[.!?]\s+", text.strip())
print("Sentences:", sentences)
# -> ['Emails: alice@example.com, bob.smith99@test.co.uk, not-an-email@bad, "quoted string"',
# 'Passwords: Passw0rd!, weakpass, Strong#123, NoSpecialChar1',
# 'Quotes: She said "Hello World", then "Regex is fun!" and finally "1234"']
text = """
Contact: alice@example.com, bob99@test.co.uk
Dates: 2025-09-25, 25/09/2025, 09-25-2025
Hashtags: #Python #Regex #AI
Mentions: @alice @bob_smith
HTML: <div class="content">Hello</div>
IPv4: 192.168.1.1, 10.0.0.255, not.an.ip
URLs: https://example.com, http://test.org/page?id=5
"""
# Email addresses
emails = re.findall(r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}", text)
print("Emails:", emails)
# -> ['alice@example.com', 'bob99@test.co.uk']
# Dates (YYYY-MM-DD or DD/MM/YYYY or MM-DD-YYYY)
dates = re.findall(r"\b(?:\d{4}-\d{2}-\d{2}|\d{2}[/-]\d{2}[/-]\d{4})\b", text)
print("Dates:", dates)
# -> ['2025-09-25', '25/09/2025', '09-25-2025']
# Hashtags
hashtags = re.findall(r"#\w+", text)
print("Hashtags:", hashtags)
# -> ['#Python', '#Regex', '#AI']
# Mentions (@username)
mentions = re.findall(r"@\w+", text)
print("Mentions:", mentions)
# -> ['@alice', '@bob_smith']
# HTML tags
tags = re.findall(r"<[^>]+>", text)
print("HTML Tags:", tags)
# -> ['<div class="content">', '</div>']
# IPv4 addresses
ips = re.findall(r"\b(?:\d{1,3}\.){3}\d{1,3}\b", text)
print("IP Addresses:", ips)
# -> ['192.168.1.1', '10.0.0.255']
# URLs (http/https)
urls = re.findall(r"https?://[^\s]+", text)
print("URLs:", urls)
# -> ['https://example.com,', 'http://test.org/page?id=5']
Copy to Clipboard
Toggle show comments














