Tech With Galvan
Back to Articles
TechNov 29, 2026 • 5 min read

Python Regex (re Module): A Practical Guide

Master Python's re module — search, match, findall, sub, groups, and the patterns you'll actually use. With real examples and common pitfalls.

Galvan
Galvan

Founder & Creator

Introduction

Regex is the Swiss Army knife of text processing — and Python’s re module is the standard tool. This post is the practical guide: the functions you’ll actually use, the patterns that matter, and the mistakes everyone makes.

The core functions

import re

text = "Contact: [email protected] or [email protected]"

# search: find first match anywhere
m = re.search(r"\w+@\w+\.\w+", text)
print(m.group())   # [email protected]

# findall: find all matches
emails = re.findall(r"\w+@\w+\.\w+", text)
print(emails)      # ['[email protected]', '[email protected]']

# match: match only at the start
m = re.match(r"Contact", text)
print(m.group())   # Contact

# sub: replace
clean = re.sub(r"\w+@\w+\.\w+", "[EMAIL]", text)
print(clean)       # Contact: [EMAIL] or [EMAIL]

Common patterns

Pattern Matches
\d Digit (0-9)
\w Word char (a-z, A-Z, 0-9, _)
\s Whitespace
. Any char (except newline)
* 0 or more
+ 1 or more
? 0 or 1
{n} Exactly n
{n,m} n to m
^ Start of string
$ End of string
[abc] One of a, b, c
[^abc] Not a, b, or c
| OR
() Group
(?:...) Non-capturing group

Real examples

1. Email validation

import re

EMAIL_RE = re.compile(r"^[^\s@]+@[^\s@]+\.[^\s@]+$")

def is_valid_email(email):
    return bool(EMAIL_RE.match(email))

print(is_valid_email("[email protected]"))   # True
print(is_valid_email("not-an-email"))         # False

2. Extract URLs

URL_RE = re.compile(r"https?://[^\s]+")
text = "Visit https://techwithgalvan.in and http://example.com/path?q=1"
urls = URL_RE.findall(text)
# ['https://techwithgalvan.in', 'http://example.com/path?q=1']

3. Parse log lines

LOG_RE = re.compile(r"(\d{4}-\d{2}-\d{2}) (\w+) (.*)")
line = "2026-03-08 INFO User logged in"
m = LOG_RE.match(line)
date, level, msg = m.groups()
# date='2026-03-08', level='INFO', msg='User logged in'

4. Clean text

# Remove extra whitespace
text = "Hello   world\n\n\nfoo"
clean = re.sub(r"\s+", " ", text).strip()
# "Hello world foo"

# Remove HTML tags
html = "<p>Hello <b>world</b></p>"
text = re.sub(r"<[^>]+>", "", html)
# "Hello world"

5. Named groups

m = re.search(r"(?P<name>\w+)@(?P<domain>\w+\.\w+)", "[email protected]")
print(m.group("name"))     # galvan
print(m.group("domain"))   # example.com
print(m.groupdict())       # {'name': 'galvan', 'domain': 'example.com'}

Named groups make extraction self-documenting.

Compiled patterns (performance)

# Compile once, reuse many times
EMAIL_RE = re.compile(r"^[^\s@]+@[^\s@]+\.[^\s@]+$")

# Use in a loop
for email in emails:
    if EMAIL_RE.match(email):
        valid.append(email)

Always re.compile() patterns you use repeatedly — it avoids re-parsing the regex each call.

Flags

re.IGNORECASE   # case-insensitive
re.MULTILINE    # ^ and $ match per line
re.DOTALL       # . matches newlines too
re.VERBOSE      # allow comments and whitespace in pattern

# Combine
re.compile(r"pattern", re.IGNORECASE | re.MULTILINE)

VERBOSE for readable patterns

PHONE_RE = re.compile(r"""
    ^(\+91)?          # optional country code
    [\s-]?            # optional separator
    (\d{10})          # 10-digit number
    $""", re.VERBOSE)

Common mistakes

  • Forgetting raw strings — r"\d+" not "\d+". Without r, \d becomes an escape sequence.
  • Greedy matching — .* matches as much as possible. Use .*? for non-greedy.
  • Not anchoring — re.search finds anywhere. Use ^...$ for full-string validation.
  • Overusing regex — for simple string ops, use str.split(), str.replace(), in. Regex is for patterns.
  • Not compiling — re.findall(pattern, text) recompiles every call. Compile once.

Key Concepts

  • re.search = find first match anywhere
  • re.findall = find all matches
  • re.sub = replace matches
  • re.compile = pre-compile for reuse
  • Groups () = capture parts of the match
  • Named groups (?P<name>...) = self-documenting captures
  • Raw strings r"..." = avoid escape issues

What to Try Next

FAQ

Regex vs string methods — which to use?

For simple ops ("@" in email, text.split(",")), use string methods. For patterns (emails, phones, log parsing), use regex. When in doubt, try string methods first.

How do I test regex?

Use regex101.com — paste your pattern and test strings, see matches in real time. Python’s re flavor is supported.

Is regex slow?

Compiled regex is fast. Backtracking (nested quantifiers like (a+)+) can be exponential — avoid nested */+. For most patterns, performance is fine.