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 Aug 17 2026
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 reThe 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 retext = "Python 3.12 is released in 2023!"# Searchmatch = 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)# Fullmatchprint("Fullmatch:", re.fullmatch(r".+", text) is not None)# -> True (entire string matches ".+")# Findallprint("Findall digits:", re.findall(r"\d+", text))# -> ['3', '12', '2023']# Finditerfor 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)# Substitutionprint("Sub:", re.sub(r"\d+", "#", text))# -> "Python #.# is released in #!"# Splitprint("Split:", re.split(r"\s+", text))# -> ['Python', '3.12', 'is', 'released', 'in', '2023!']# Compile for reusepattern = re.compile(r"\d+")print("Compile Findall:", pattern.findall(text))# -> ['3', '12', '2023']# Example texttext = '''Emails: alice@example.com, bob.smith99@test.co.uk, not-an-email@bad, "quoted string"Passwords: Passw0rd!, weakpass, Strong#123, NoSpecialChar1Quotes: She said "Hello World", then "Regex is fun!" and finally "1234"'''# Find all email addressesemails = 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 quotesquotes = 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 charpassword_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 letterscapitals = 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.ukDates: 2025-09-25, 25/09/2025, 09-25-2025Hashtags: #Python #Regex #AIMentions: @alice @bob_smithHTML: <div class="content">Hello</div>IPv4: 192.168.1.1, 10.0.0.255, not.an.ipURLs: https://example.com, http://test.org/page?id=5"""# Email addressesemails = 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']# Hashtagshashtags = 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 tagstags = re.findall(r"<[^>]+>", text)print("HTML Tags:", tags)# -> ['<div class="content">', '</div>']# IPv4 addressesips = 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']