🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

re Module

AI & DATA SCIENCE // re-module

The re module provides regular expression matching, letting you search, extract, and replace text based on pattern rules rather than exact substrings.

Syntax

import re
re.match(pattern, text)
re.search(pattern, text)
re.findall(pattern, text)
re.sub(pattern, repl, text)

Deep Dive Course

A regular expression is a compact pattern language for describing text shapes — digits, whitespace, repetitions, alternatives — rather than literal substrings. re.match() checks for a match only at the very start of the string, re.search() looks for a match anywhere within it, re.findall() returns every non-overlapping match as a list, and re.sub() replaces matches with a given replacement. Patterns used repeatedly should be compiled once with re.compile(pattern) into a pattern object, which is more efficient than passing the same raw pattern string to a re function on every call.

1Understanding re Module

A regular expression is a compact pattern language for describing text shapes — digits, whitespace, repetitions, alternatives — rather than literal substrings. re.match() checks for a match only at the very start of the string, re.search() looks for a match anywhere within it, re.findall() returns every non-overlapping match as a list, and re.sub() replaces matches with a given replacement. Patterns used repeatedly should be compiled once with re.compile(pattern) into a pattern object, which is more efficient than passing the same raw pattern string to a re function on every call.

💡

Compile a regex once with re.compile() and reuse the resulting pattern object if you're applying the same pattern many times in a loop — recompiling the same pattern string repeatedly wastes work, since Python caches only a small number of recently-used raw patterns internally.

editor.html
import re

text = "Contact: alice@example.com or bob@example.com"
emails = re.findall(r"[\w.]+@[\w.]+", text)
print(emails)
localhost:3000

2Practical Example

Here is a real-world application of re Module showing how it is used in production Python code.

editor.html
import re

text = "Phone: 123-456-7890"
masked = re.sub(r"\d", "*", text)
print(masked)
localhost:3000

3Best Practices

Follow these guidelines when working with re Module:

1. Compile a pattern with re.compile() when it's used repeatedly, instead of passing the raw string to a re function every time

2. Use raw strings for regex patterns, so backslashes aren't also interpreted as Python string escape sequences

3. Prefer simple string methods like str.split() or the in operator for straightforward text checks — reach for regex only once the matching logic genuinely needs patterns, not just literal text

⚠️

Tip: Compile a regex once with re.compile() and reuse the resulting pattern object if you're applying the same pattern many times in a loop — recompiling the same pattern string repeatedly wastes work, since Python caches only a small number of recently-used raw patterns internally.

editor.html
import re

text = "Contact: alice@example.com or bob@example.com"
emails = re.findall(r"[\w.]+@[\w.]+", text)
print(emails)
localhost:3000

Examples

Example 01Basic Usage
import re

text = "Contact: alice@example.com or bob@example.com"
emails = re.findall(r"[\w.]+@[\w.]+", text)
print(emails)
Example 02Advanced Example
import re

text = "Phone: 123-456-7890"
masked = re.sub(r"\d", "*", text)
print(masked)

Best Practices

  • Compile a pattern with re.compile() when it's used repeatedly, instead of passing the raw string to a re function every time
  • Use raw strings for regex patterns, so backslashes aren't also interpreted as Python string escape sequences
  • Prefer simple string methods like str.split() or the in operator for straightforward text checks — reach for regex only once the matching logic genuinely needs patterns, not just literal text

Interview Question

Why should regex patterns in Python usually be written as raw strings?

Hint: Think about how backslashes are interpreted twice: once by Python, once by the regex engine.

Without the raw-string prefix, Python's own string literal parser processes backslash escape sequences first, before the regex engine ever sees the string — so a pattern meant to represent a regex digit escape would first be interpreted by Python itself, which requires doubling backslashes or produces unexpected results for sequences Python doesn't recognize. A raw string tells Python not to process backslash escapes at all, passing them through unchanged so the regex engine can interpret them exactly as intended.

Exercises

MediumPractice using re Module in a real scenario.
View Solution
import re

text = "Contact: alice@example.com or bob@example.com"
emails = re.findall(r"[\w.]+@[\w.]+", text)
print(emails)

Frequently Asked Questions

Why should regex patterns in Python usually be written as raw strings?

Without the raw-string prefix, Python's own string literal parser processes backslash escape sequences first, before the regex engine ever sees the string — so a pattern meant to represent a regex digit escape would first be interpreted by Python itself, which requires doubling backslashes or produces unexpected results for sequences Python doesn't recognize. A raw string tells Python not to process backslash escapes at all, passing them through unchanged so the regex engine can interpret them exactly as intended.

Related Functions

stringslist-comprehensionswith-statement