Course topics

By WebNest Studio Engineering Team

Python Tutorial

Regular Expressions in Python

A regular expression (regex) is a pattern that describes text: "a 6-digit PIN code", "an email address", "a date like 27-09-2026". Python's re module uses these patterns to search, validate, extract and replace text — tasks that would need many lines of string methods.

This lesson covers pattern syntax, the main re functions (match, search, fullmatch, findall, finditer, sub, split), groups and named groups, flags, compiling patterns, and practical validation and extraction examples.

Why it matters

Validating an email address or phone number, pulling dates out of log lines and cleaning messy text are pattern-matching tasks. A regular expression describes the pattern in one line where string methods would need many. They are powerful and easy to get wrong, so knowing the basic syntax and the main functions of re matters.

Pattern Syntax

The essentials:

  • Characters: . any character, \d digit, \w word character, \s whitespace (capitals negate: \D, \W, \S).
  • Sets: [aeiou], ranges [a-z0-9], negation [^0-9].
  • Quantifiers: * (0+), + (1+), ? (0 or 1), {3}, {2,4}; add ? for non-greedy (.*?).
  • Anchors: ^ start, $ end, \b word boundary.
  • Groups: ( ) capture, (?P<name> ) named, (?: ) non-capturing, | alternation; lookarounds (?= ), (?! ).

Raw Strings

Always write patterns as raw strings — r"\d+" — so Python does not interpret backslashes before the regex engine sees them.

re Functions

re.search finds the first match anywhere; re.match only at the start; re.fullmatch requires the whole string to match (best for validation). re.findall returns all matches (or groups) as a list; re.finditer yields match objects; re.sub replaces (the replacement can reference groups or be a function); re.split splits on a pattern. Match objects provide group(), groups(), groupdict(), start() and end().

Flags and Compiling

re.IGNORECASE, re.MULTILINE (^/$ per line), re.DOTALL (. matches newlines) and re.VERBOSE (allow whitespace and comments in patterns). re.compile(pattern) creates a reusable pattern object — clearer when a pattern is used many times.

Examples

search, match, fullmatch, findall and finditer

Python
import re

text = "Order WN-1042 shipped on 2026-09-27; order WN-1043 on 2026-09-30."
print(re.search(r"WN-\d+", text).group())
print(re.match(r"WN-\d+", text), re.match(r"Order", text).group())
print(bool(re.fullmatch(r"\d{6}", "411001")), bool(re.fullmatch(r"\d{6}", "41100")))
print(re.findall(r"WN-(\d+)", text))
print(re.findall(r"(\d{4})-(\d{2})-(\d{2})", text))
for m in re.finditer(r"\d{4}-\d{2}-\d{2}", text):
    print(m.group(), m.start(), m.end())
Output
WN-1042
None Order
True False
['1042', '1043']
[('2026', '09', '27'), ('2026', '09', '30')]
2026-09-27 25 35
2026-09-30 54 64

Named groups, sub with backreferences and functions, split

Python
import re

log = "2026-09-27 10:15:02 ERROR payment failed user=asha amount=2999"
pattern = re.compile(r"(?P<date>\S+) (?P<time>\S+) (?P<level>[A-Z]+) (?P<msg>.*)")
m = pattern.match(log)
print(m.group("level"), "|", m.groupdict()["msg"])

print(re.sub(r"(\d{4})-(\d{2})-(\d{2})", r"\3/\2/\1", "Due 2026-09-27"))
print(re.sub(r"\d{4}(?=\d{4}$)", "****", "Card 1234567812345678"))
print(re.sub(r"\d+", lambda m: str(int(m.group()) * 2), "3 pens and 10 books"))
print(re.split(r"[,;\s]+", "python, java;  sql go"))
print(re.sub(r"\s+", " ", "  too    many   spaces ").strip())
Output
ERROR | payment failed user=asha amount=2999
Due 27/09/2026
Card 12345678****5678
6 pens and 20 books
['python', 'java', 'sql', 'go']
too many spaces

Validating common Indian and web formats

Python
import re

validators = {
    "email": re.compile(r"[\w.+-]+@[\w-]+(\.[\w-]+)+"),
    "mobile": re.compile(r"(\+91[\s-]?)?[6-9]\d{9}"),
    "pincode": re.compile(r"[1-9]\d{5}"),
    "pan": re.compile(r"[A-Z]{5}\d{4}[A-Z]"),
    "strong_password": re.compile(r"(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\w\s]).{8,}"),
}
samples = {
    "email": ["asha@webnest.in", "bad@", "a.b+tag@mail.co.uk"],
    "mobile": ["9876543210", "+91 9876543210", "1234567890"],
    "pincode": ["411001", "011001"],
    "pan": ["ABCDE1234F", "ABCD1234F"],
    "strong_password": ["Passw0rd!", "password"],
}
for kind, values in samples.items():
    results = [f"{v}={'ok' if validators[kind].fullmatch(v) else 'no'}" for v in values]
    print(f"{kind:<16}", ", ".join(results))
Output
email            asha@webnest.in=ok, bad@=no, a.b+tag@mail.co.uk=ok
mobile           9876543210=ok, +91 9876543210=ok, 1234567890=no
pincode          411001=ok, 011001=no
pan              ABCDE1234F=ok, ABCD1234F=no
strong_password  Passw0rd!=ok, password=no

Flags: IGNORECASE, MULTILINE, DOTALL and VERBOSE

Python
import re

print(re.findall(r"python", "Python PYTHON python", flags=re.IGNORECASE))
notes = "TODO: write tests\nnote: fine\nTODO: deploy"
print(re.findall(r"^TODO: (.*)$", notes, flags=re.MULTILINE))
html = "<p>line one\nline two</p>"
print(re.search(r"<p>(.*)</p>", html), re.search(r"<p>(.*)</p>", html, re.DOTALL).group(1).split("\n"))
print(re.findall(r"<.+?>", "<b>bold</b>"), re.findall(r"<.+>", "<b>bold</b>"))

price = re.compile(r"""
    (?P<currency>Rs\.|₹)\s*   # currency symbol
    (?P<amount>[\d,]+)        # digits with commas
""", re.VERBOSE)
m = price.search("Total: ₹ 1,299 only")
print(m.group("currency"), int(m.group("amount").replace(",", "")))
Output
['Python', 'PYTHON', 'python']
['write tests', 'deploy']
None ['line one', 'line two']
['<b>', '</b>'] ['<b>bold</b>']
₹ 1299

Common Mistakes

  • Writing patterns without raw strings, so "\b" becomes a backspace character.
  • Using re.match when you meant re.search (match only checks the start).
  • Validating with search instead of fullmatch, accepting strings that merely contain a valid part.
  • Greedy .* swallowing too much — use non-greedy .*? or a more specific class.
  • Parsing HTML or complex formats with regex instead of a proper parser.

Exercise

Find every email address in the text. Then check whether each of two strings is a valid six-digit PIN code that does not start with 0, printing both results on one line. Finally replace every digit in a phone message with #.

Expected output: ['asha@example.com', 'ravi@test.org'], True False, Call ##### (one per line)

Python · starter code
import re

text = "Contact asha@example.com or ravi@test.org today"
# TODO: print a list of the email addresses in text

# TODO: print whether "110001" and "012345" are valid PIN codes, on one line

# TODO: print "Call 98765" with every digit replaced by "#"
Show hints
  • re.findall(pattern, text) returns all matches. A simple email pattern is r"[\w.+-]+@[\w-]+\.[\w.]+".
  • re.fullmatch(r"[1-9]\d{5}", value) matches only if the whole string fits; re.sub(r"\d", "#", text) replaces each digit.
Show solution
Python · solution
import re

text = "Contact asha@example.com or ravi@test.org today"
print(re.findall(r"[\w.+-]+@[\w-]+\.[\w.]+", text))

pin = r"[1-9]\d{5}"
print(bool(re.fullmatch(pin, "110001")), bool(re.fullmatch(pin, "012345")))

print(re.sub(r"\d", "#", "Call 98765"))

Quick quiz

1. What is the difference between re.match and re.search?

  • A. There is none
  • B. search looks only at the start
  • C. match looks only at the start of the string; search looks anywhere in it
  • D. match returns a list
Show answer

C. fullmatch requires the whole string to match the pattern.

2. Why are patterns written as raw strings, such as r"\d+"?

  • A. They run faster
  • B. It is required by the re module
  • C. So that backslashes reach the regex engine unchanged
  • D. To allow Unicode
Show answer

C. In a normal string, Python would process the backslash sequences first.

3. What does the pattern \d{3} match?

  • A. The letter d three times
  • B. Up to three digits
  • C. Three or more digits
  • D. Exactly three digits
Show answer

D. \d is a digit and {3} repeats it exactly three times.

Interview questions

What is the difference between greedy and lazy matching?

Show answer

A greedy quantifier such as .* matches as much as it can while still allowing the rest of the pattern to match. Adding ? makes it lazy, .*?, so it matches as little as possible. Applied to <b>one</b><b>two</b>, the pattern <b>.*</b> matches the whole text, while <b>.*?</b> matches each pair of tags separately.

When should you compile a regular expression?

Show answer

re.compile(pattern) returns a pattern object whose methods can be called repeatedly. It is worth doing when the same pattern is used many times, for example on every line of a file, and it also lets the pattern be given a name and defined in one place. The re module caches recently used patterns, so for occasional use the plain functions are fine.

Key Points to Remember

  • Write patterns as raw strings: r"\d+".
  • search (anywhere), match (start), fullmatch (whole string), findall/finditer (all), sub, split.
  • Groups, named groups and backreferences extract and rearrange text.
  • Flags: IGNORECASE, MULTILINE, DOTALL, VERBOSE; compile reused patterns.

Practice in the playground

Write and run your own code for this topic, or start from the exercise or an example. Runs Python in your browser. Your code for each topic is kept in this browser.

Run the examples

Change an input, predict the result, then compare it with the output. Explain why the result changes.

WebNest Studio

Written by

WebNest Studio Engineering Team

Practical lessons from developers working with Java, Python, React, databases and production systems.

Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering