The re Module in Python
The re module is Python’s built-in library for working with regular expressions. It provides everything you need to search, match, extract, split, and substitute text patterns — the foundation of modern text processing in data science. Whether you are cleaning logs, extracting features from unstructured data, validating inputs, or building NLP pipelines, mastering the re module is essential in 2026.
TL;DR — Most Important re Functions
re.search()→ find first matchre.match()→ match at start of stringre.findall()→ return all matches as listre.sub()→ replace matchesre.split()→ split on patternre.compile()→ pre-compile for speed
1. Importing and Basic Usage
import re
text = "Order ORD-98765 placed for $1,250.75 on 2026-03-19"
# Simple search
match = re.search(r"ORD-(\d+)", text)
if match:
print("Order ID:", match.group(1))
# Find all numbers
numbers = re.findall(r"\d+", text)
print(numbers)
2. Core Functions with Examples
# re.match() - only at beginning
print(re.match(r"Order", text))
# re.sub() - substitution
clean = re.sub(r"\$\d+(?:,\d+)?(?:\.\d+)?", "[PRICE]", text)
print(clean)
# re.split() - split on pattern
parts = re.split(r"\s+", text)
print(parts)
3. Real-World Data Science with Pandas
import pandas as pd
# Demo file so this example runs as-is (in your project this is your real log export)
pd.DataFrame({"log": [
"2026-03-19 ERROR: Payment failed for ORD-12345 amount $1,250.75 Qty: 3 user alice@example.com!!! https://shop.example.com/o/12345 call +1 555-123-4567",
"2026-03-19 WARNING: Slow response for ORD-98765 amount $87.50 Qty: 1 bob@company.com on 03/19/2026 aaaa",
"INFO: user logged in ORD-555 amount $12.00 Qty: 2 carol@data.org <b>ok</b>",
]}).to_csv("logs.csv", index=False)
# Demo file so this example runs as-is (in your project this is your real log export)
pd.DataFrame({"log": [
"2026-03-19 ERROR: Payment failed for ORD-12345 amount $1,250.75 Qty: 3 user alice@example.com!!! https://shop.example.com/o/12345 call +1 555-123-4567",
"2026-03-19 WARNING: Slow response for ORD-98765 amount $87.50 Qty: 1 bob@company.com on 03/19/2026 aaaa",
"INFO: user logged in ORD-555 amount $12.00 Qty: 2 carol@data.org <b>ok</b>",
]}).to_csv("logs.csv", index=False)
df = pd.read_csv("logs.csv")
# Vectorized extraction
df["order_id"] = df["log"].str.extract(r"ORD-(\d+)")[0]
# Vectorized substitution
df["clean_log"] = df["log"].str.replace(r"\$[\d,]+\.\d{2}", "[AMOUNT]", regex=True)
# Vectorized split
df["tokens"] = df["log"].str.split(r"\s+", regex=True)
print(df["order_id"].tolist())
4. Compilation, Flags & Best Practices in 2026
# Pre-compile for performance on large datasets
pattern = re.compile(r"ORD-(\d+)", re.IGNORECASE)
# Use with flags
matches = pattern.findall("ord-12345 ORD-98765")
print(matches)
5. Best Practices in 2026
- Always use raw strings
r"..."for patterns - Pre-compile patterns used more than once
- Prefer pandas
.strmethods for DataFrame-scale work - Use
re.VERBOSE(or inline(?x)) for complex patterns - Combine with
re.sub()callables for dynamic transformations
Compile Once, Reuse Many Times
re.compile turns a pattern into a pattern object with methods such as search, match, fullmatch, findall, finditer, sub and split. The module-level functions cache recent patterns, so compiling is less about speed and more about clarity: one named object documents what the pattern is for.
match checks only the start of the string, search looks anywhere, and fullmatch requires the whole string to match. Choosing the wrong one is a common cause of validation bugs.
Compiled patterns also accept flags once, so every later call uses the same settings.
Worked Example: Validating IDs
The same compiled pattern is used with match, search and fullmatch.
import re
ID = re.compile(r"[A-Z]{2}\d{4}")
for value in ["PK2026", "PK2026x", "xPK2026"]:
print(value,
bool(ID.match(value)),
bool(ID.search(value)),
bool(ID.fullmatch(value)))
Only "PK2026" passes fullmatch. match accepts "PK2026x" because it checks only the start, and search accepts all three, which shows why validation should use fullmatch.
Practical Tips
Name compiled patterns in capital letters, such as EMAIL or ORDER_ID, to show they are constants.
Common Mistakes
- Using match for validation and accepting trailing text.
- Calling .group() on None when nothing matched.
- Recompiling the same pattern inside loops for no reason.
- Forgetting raw strings in compiled patterns.
Quick Summary
- re.compile names and reuses a pattern.
- match checks the start only.
- fullmatch validates the whole string.
- search finds a match anywhere.
Conclusion
The re module is the heart of all regular-expression work in Python. In 2026 data science projects it powers log cleaning, feature extraction, data anonymization, and text standardization at scale. Master its core functions, compilation, flags, and pandas integration, and you will be ready to tackle any text-processing challenge with speed and precision.
Next steps:
- Open one of your current text-processing scripts and rewrite the pattern handling using the
remodule functions shown above