Tutorials Python Python Regular Expressions
Ch 48 / 58
Python Intermediate 14 min read Chapter 48 of 58

Python Regular Expressions

Regular expressions (regex) are a powerful mini-language for describing text patterns. With Python's re module you can search for patterns in text, extract data, validate inputs (emails, phone numbers, URLs), and replace or split strings in ways that plain string methods cannot match. From parsing log files to validating web forms, regex is an essential tool in every Python developer's toolkit.

What you'll learn in this chapter
Regex syntax — characters, classes, quantifiers, anchors
re module — search, match, findall, fullmatch
Groups — capturing, named groups, non-capturing
Substitution — sub() and subn()
Flags — IGNORECASE, MULTILINE, DOTALL
Real-world patterns — email, phone, URL validation
Advertisement

Regex Syntax — Pattern Building Blocks

A regex pattern is a string of special characters. Each character or group of characters matches something specific in the target text. Here are the core building blocks:

Core regex syntax — symbols, classes, quantifiers & anchors
^ $
Anchors — position in string
^ = start of string/line  ·  $ = end of string/line  ·  \b = word boundary
r"^hello" matches "hello world" but NOT "say hello"
\d \w .
Character classes — special sequences
\d=digit [0-9]  ·  \D=non-digit  ·  \w=[a-zA-Z0-9_]  ·  \s=whitespace  ·  .=any char except \n
r"\d{3}" matches "123" in "call 123-4567"
* + ?
Quantifiers — how many times
*=0 or more  ·  +=1 or more  ·  ?=0 or 1  ·  {n}=exactly n  ·  {m,n}=m to n times
r"\d{4}" matches exactly 4 digits like "2025"
( ) [ ]
Groups & sets — capture and match sets
(abc)=capture group  ·  [abc]=any of a, b, c  ·  [^abc]=NOT a, b, c  ·  a|b=a OR b
r"[aeiou]" matches any vowel in a string
\ r""
Escape & raw strings
\ escapes a special char. Always use r"..." raw strings for regex — avoids double-escaping Python and regex backslashes.
r"\." matches literal dot, "." matches any char

The re Module — Key Functions

Python's re module provides all regex functions. Here are the most important ones:

re.search()
→ Match | None
Find first match anywhere in string
re.match()
→ Match | None
Match only at start of string
re.fullmatch()
→ Match | None
Pattern must match entire string
re.findall()
→ list
All non-overlapping matches as list
re.finditer()
→ iterator
Iterator of match objects
re.sub()
→ str
Replace matches with string
re.split()
→ list
Split string at each match
re.compile()
→ Pattern
Pre-compile for repeated use
Python — re_functions.py
import re

text = "The price is ₹499 and ₹1299 today"

# re.search() — first match anywhere
m = re.search(r"\d+", text)
print(m)                            # <re.Match object; span=(13, 16)>
print(m.group())                    # 499
print(m.span())                     # (13, 16) — start/end positions
print(m.start(), m.end())           # 13 16

# re.findall() — ALL matches as list
prices = re.findall(r"\d+", text)
print(prices)                       # ['499', '1299']

# re.match() — only at start
print(re.match(r"The", text))       # Match object — starts with "The"
print(re.match(r"price", text))    # None — "price" not at start

# re.fullmatch() — entire string must match
print(re.fullmatch(r"\d{4}", "2025"))   # Match
print(re.fullmatch(r"\d{4}", "202"))    # None — not 4 digits

# re.finditer() — iterator of match objects
for m in re.finditer(r"\d+", text):
    print(f"Found {m.group()} at {m.span()}")
# Found 499 at (13, 16)
# Found 1299 at (24, 28)

# re.split() — split on pattern
parts = re.split(r[,;\s]+", "one,two; three  four")
print(parts)                        # ['one', 'two', 'three', 'four']

# Always check for None before using match result!
if m := re.search(r"\d+", text):   # walrus operator
    print(f"First number: {m.group()}")

Regex Patterns — Deep Dive

Understanding character classes, quantifiers, anchors and lookaheads unlocks the full power of regex:

Python — regex_patterns.py
import re

# ── Character classes ─────────────────────────────────
print(re.findall(r"\d+", "abc 123 def 456"))      # ['123', '456']
print(re.findall(r"\w+", "Hello World! 42"))       # ['Hello', 'World', '42']
print(re.findall(r"\s+", "a  b\tc"))               # ['  ', '\t']
print(re.findall(r".",  "abc"))                    # ['a','b','c'] — any char

# ── Custom character sets ─────────────────────────────
print(re.findall(r"[aeiou]",  "hello world"))     # ['e','o','o']
print(re.findall(r"[^aeiou\s]", "hello"))          # ['h','l','l'] — non-vowel
print(re.findall(r"[a-f0-9]",  "3g5h2"))           # ['3','5','2'] — hex chars

# ── Quantifiers ───────────────────────────────────────
print(re.findall(r"ab*",  "a ab abb abbb"))       # ['a','ab','abb','abbb']
print(re.findall(r"ab+",  "a ab abb abbb"))       # ['ab','abb','abbb'] — 1+
print(re.findall(r"ab?",  "a ab abb"))             # ['a','ab','ab']
print(re.findall(r"\d{2,4}", "1 12 123 1234 12345"))
# ['12','123','1234','1234'] — greedy match

# ── Anchors ───────────────────────────────────────────
print(re.findall(r"\bword\b", "word wording sword")) # ['word']
print(re.findall(r"^\w+", "hello world"))            # ['hello']
print(re.findall(r"\w+$", "hello world"))            # ['world']

# ── Greedy vs non-greedy ──────────────────────────────
html = "<b>bold</b> and <i>italic</i>"
print(re.findall(r"<.*>",  html))       # greedy  → ['<b>bold</b> and <i>italic</i>']
print(re.findall(r"<.*?>", html))       # lazy    → ['<b>', '</b>', '<i>', '</i>']
Advertisement
K2infocom Python Masterclass & Backend Development
Master Python, Django & Backend APIs with AI
Go from absolute beginner to building production-ready REST APIs, web applications, and ML pipelines. Real projects, live mentoring in Vadodara, and placement support included.
2,400+
Students Enrolled
4.9 ⭐
Google Rating
6 Months
Full Program
100%
Placement Support
Python 3 Django & DRF FastAPI PostgreSQL Machine Learning Docker & AWS Real Projects AI Tools

Groups — Capturing & Named Groups

Groups let you capture specific parts of a match. Parentheses () create a capturing group. You can reference captured groups by number (group(1)) or by name using (?P<name>...).

Python — regex_groups.py
import re

# ── Numbered groups — group(0) = whole match ─────────
m = re.search(r"(\d{4})-(\d{2})-(\d{2})", "Date: 2025-05-16")
print(m.group())            # 2025-05-16  (whole match)
print(m.group(1))           # 2025        (year)
print(m.group(2))           # 05          (month)
print(m.group(3))           # 16          (day)
print(m.groups())           # ('2025', '05', '16')

# ── Named groups — ?P<name> ──────────────────────────
pattern = r"(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})"
m = re.search(pattern, "Date: 2025-05-16")

print(m.group("year"))       # 2025
print(m.group("month"))      # 05
print(m.groupdict())         # {'year': '2025', 'month': '05', 'day': '16'}

# ── findall with groups ────────────────────────────────
text = "alice@k2.com, bob@gmail.com"
emails = re.findall(r"(\w+)@(\w+\.\w+)", text)
print(emails)               # [('alice', 'k2.com'), ('bob', 'gmail.com')]

# ── Non-capturing group  (?:...) ──────────────────────
m = re.search(r"(?:https?|ftp)://([\w.]+)", "https://k2infocom.com")
print(m.group(1))           # k2infocom.com  (domain only, NOT "https")

# ── Backreference — \1 refers to group 1 ─────────────
m = re.search(rr"(\w+) \1", "hello hello world")
print(m.group())             # hello hello — finds repeated word

Substitution — re.sub() and re.subn()

re.sub(pattern, replacement, string) replaces all matches. The replacement can be a string (with \1 backreferences) or a callable that receives a match object and returns the replacement string.

Python — regex_substitution.py
import re

# ── Basic substitution ────────────────────────────────
text = "Hello World! Hello Python!"
print(re.sub(r"Hello", "Hi", text))
# Hi World! Hi Python!

# ── Limit number of replacements ─────────────────────
print(re.sub(r"Hello", "Hi", text, count=1))
# Hi World! Hello Python!  ← only first replaced

# ── Use group backreferences in replacement ───────────
date = "2025-05-16"
us_date = re.sub(rr"(\d{4})-(\d{2})-(\d{2})",
                  rr"\2/\3/\1", date)
print(us_date)                   # 05/16/2025

# ── Named group in replacement ────────────────────────
print(re.sub(rr"(?P<last>\w+), (?P<first>\w+)",
              rr"\g<first> \g<last>",
              "Smith, John"))          # John Smith

# ── Callable replacement ─────────────────────────────
def double_num(m):
    return str(int(m.group()) * 2)

print(re.sub(rr"\d+", double_num, "2 cats and 5 dogs"))
# 4 cats and 10 dogs

# ── re.subn() — returns (new_string, count) ───────────
result, n = re.subn(rr"\d+", "NUM", "I have 3 cats and 5 dogs")
print(result, f"({n} replacements)")
# I have NUM cats and NUM dogs (2 replacements)

# ── Clean up whitespace ───────────────────────────────
messy  = "  Too   many   spaces  here  "
clean  = re.sub(rr"\s+", " ", messy).strip()
print(clean)                     # Too many spaces here

Flags & Compiling Patterns

Flags modify how regex patterns are interpreted. Pre-compile patterns with re.compile() for performance when you use the same pattern many times.

Python — regex_flags.py
import re

# ── re.IGNORECASE (re.I) ──────────────────────────────
print(re.findall(r"python", "Python PYTHON python", re.I))
# ['Python', 'PYTHON', 'python']

# ── re.MULTILINE (re.M) — ^ and $ match each line ────
text = "first line\nsecond line\nthird line"
print(re.findall(rr"^\w+", text, re.M))
# ['first', 'second', 'third']

# ── re.DOTALL (re.S) — dot matches newline too ────────
html = "<p>\n  Content\n</p>"
print(re.search(r"<p>.+</p>",   html))           # None — . doesn't match \n
print(re.search(r"<p>.+</p>",   html, re.S))    # Match!

# ── Combine flags with | ──────────────────────────────
print(re.findall(rr"^\w+", "HELLO\nWORLD", re.I | re.M))
# ['HELLO', 'WORLD']

# ── Inline flags inside pattern ───────────────────────
print(re.findall(r"(?i)python", "Python PYTHON"))
# ['Python', 'PYTHON']

# ── re.compile() — pre-compile for reuse ─────────────
email_pat = re.compile(
    rr"[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+"
)

logs = ["user alice@k2.com logged in", "no email here", "bob@gmail.com"]
for log in logs:
    m = email_pat.search(log)
    if m:
        print(f"Email: {m.group()}")
# Email: alice@k2.com
# Email: bob@gmail.com

# ── Verbose mode re.X — readable patterns ────────────
phone_pat = re.compile(rr"""
    (?:\+91)?       # optional India code
    \s?             # optional space
    [6-9]           # mobile starts with 6-9
    \d{9}           # 9 more digits
    "", re.X)

print(bool(phone_pat.fullmatch("9876543210")))   # True

Real-World Regex Patterns

These are the most common regex patterns used in production Python applications — validation, extraction, and text processing:

Python — real_world_regex.py
import re

# ── Email validation ──────────────────────────────────
def is_valid_email(email: str) -> bool:
    pattern = rr"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z]{2,}$"
    return bool(re.fullmatch(pattern, email))

print(is_valid_email("alice@k2infocom.com"))  # True
print(is_valid_email("not-an-email"))         # False

# ── Indian mobile number ──────────────────────────────
def is_valid_mobile(phone: str) -> bool:
    pattern = rr"^(?:\+91|0)?[6-9]\d{9}$"
    return bool(re.fullmatch(pattern, phone))

print(is_valid_mobile("9876543210"))          # True
print(is_valid_mobile("+919876543210"))        # True

# ── URL extraction ────────────────────────────────────
text = "Visit https://k2infocom.com and http://docs.python.org"
urls = re.findall(rr"https?://[\w./%-]+", text)
print(urls)               # ['https://k2infocom.com', 'http://docs.python.org']

# ── Parse a log line ──────────────────────────────────
log     = "2025-05-16 14:32:01 ERROR 404 /api/users user=alice"
pattern = rr"(?P<date>\d{4}-\d{2}-\d{2}) (?P<time>\d{2}:\d{2}:\d{2}) (?P<level>\w+) (?P<code>\d+) (?P<path>\S+)"
m       = re.search(pattern, log)
print(m.groupdict())
# {'date': '2025-05-16', 'time': '14:32:01', 'level': 'ERROR', 'code': '404', 'path': '/api/users'}

# ── Mask sensitive data ───────────────────────────────
def mask_card(text):
    return re.sub(rr"\b\d{4}[- ]?\d{4}[- ]?\d{4}[- ]?(\d{4})\b",
                   rr"****-****-****-\1", text)

print(mask_card("Card: 4111 1111 1111 1234"))
# Card: ****-****-****-1234

# ── Strip HTML tags ───────────────────────────────────
html  = "<p><b>Hello</b> <em>World</em></p>"
plain = re.sub(rr"<[^>]+>", "", html)
print(plain)              # Hello World

Practice Python Online — Free Compilers

Don't want to install anything yet? Practice all regular expression examples from this chapter directly in your browser with these free Python environments.

▶
Online Python 3 Compiler
Write, run and debug Python code in your browser. No installation required. Test every regex example instantly.
Open Compiler
Replit
Full IDE online
Google Colab
Free GPU notebooks
JupyterLite
Notebooks in browser
Programiz
Beginner-friendly
Chapter Summary
  • Always use raw strings r"..." for regex patterns — avoids double-escaping Python and regex backslashes.
  • Core syntax: \d (digit), \w (word char), \s (whitespace), . (any char), ^/$ (anchors), [abc] (character set), */+/?/{n,m} (quantifiers).
  • re.search() finds first match anywhere; re.match() only at start; re.fullmatch() entire string; re.findall() all matches as list.
  • Match objects: .group() whole match, .group(n) nth group, .groups() all groups, .span() positions. Always check for None.
  • Groups: (pattern) capturing, (?P<name>pattern) named, (?:pattern) non-capturing.
  • re.sub(pattern, repl, string) replaces matches. Replacement can be a string (with \1 backrefs) or a callable receiving the match object.
  • Key flags: re.I (case-insensitive), re.M (^ and $ per line), re.S (dot matches newline), re.X (verbose/comments). Combine with |.
  • Use re.compile() for patterns used multiple times. Use ? after quantifiers for non-greedy matching.
Free Resource
Download our Python Regular Expressions Cheat Sheet
Syntax reference, re functions, groups, substitution, flags & real-world patterns — one printable page.
Get Sheet
Advertisement
Quick Check — Test Your Understanding
Question & Answer
✓ Answer

re.search() scans through the entire string and returns the first match anywhere in the string.

re.match() only looks at the very beginning of the string — it's equivalent to adding ^ at the start of your pattern.

re.search(r"world", "hello world")  # Match ✅
re.match(r"world", "hello world")   # None ❌ (not at start)
re.match(r"hello", "hello world")   # Match ✅
✓ Answer

When ? follows another quantifier like * or +, it makes the match non-greedy (lazy). By default quantifiers are greedy — they match as much as possible. Adding ? makes them match as little as possible:

html = "<b>one</b> <b>two</b>"
re.findall(r"<.*>",  html)  # ['<b>one</b> <b>two</b>'] ← greedy
re.findall(r"<.*?>", html)  # ['<b>','</b>','<b>','</b>'] ← lazy
✓ Answer

The behaviour of re.findall() changes based on groups:

  • No groups → returns list of whole match strings: ['123', '456']
  • One group → returns list of group 1 matches: ['alice', 'bob']
  • Multiple groups → returns list of tuples: [('alice', 'k2.com'), ('bob', 'gmail.com')]
✓ Answer

Python processes string escape sequences like \n, \t, \d — but regex also uses backslash sequences. Without raw strings, you'd need to double every backslash:

# Without raw string — ugly double escaping:
re.search("\\d+", text)   # works but messy

# With raw string — clean and readable:
re.search(r"\d+", text)    # ✅ preferred
✓ Answer

By default ^ and $ match the start and end of the entire string. With re.MULTILINE (re.M), they match the start and end of each line:

text = "first\nsecond\nthird"
re.findall(rr"^\w+", text)        # ['first']    ← only string start
re.findall(rr"^\w+", text, re.M)  # ['first','second','third']
MCQ 1 of 3
What does \d match in a regex pattern?
MCQ 2 of 3
Which re function returns all non-overlapping matches as a list of strings?
MCQ 3 of 3
What does (?:pattern) create in a regex?

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel