Regular Expressions › Compiling Patterns

Michael BrenndoerferUpdated March 18, 20251 min read
Language AI HandbookAll chapters
Part 1 of 67Text as Data
Chapter 3Regular Expressions › Compiling Patterns
Reading section 13 of 21 · Compiling Patterns · 1 minSection 13 of 21 · 1 min

Book contents

Section 13 of 21 · 1 min read

Regular Expressions › Compiling PatternsOne section · Sections in this chapter13 / 21 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

For patterns you use repeatedly, compile them for better performance. When you call re.findall(pattern, text) directly, Python must parse the pattern string and compile it into an internal representation every time that line runs. For a pattern called once in a script this cost is negligible, but in a loop that processes millions of documents, pattern compilation overhead adds up. re.compile() performs the compilation once and returns a pattern object that you call .findall(), .search(), .sub(), etc., on directly.

In[60]:
Code
import re

# Compile once, use many times
email_regex = re.compile(
    r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b", re.IGNORECASE
)

texts = [
    "Contact support@example.com",
    "No email here",
    "Multiple: a@b.com, c@d.org",
]

results = [email_regex.findall(text) for text in texts]
Out[61]:
Console
Compiled pattern reuse:
  'Contact support@example.com'
    Found: ['support@example.com']
  'No email here'
    Found: []
  'Multiple: a@b.com, c@d.org'
    Found: ['a@b.com', 'c@d.org']

Compiled patterns also store flags, making your code cleaner when the same flags apply everywhere. A natural pattern is to compile all patterns at module load time as module-level constants. This ensures compilation happens exactly once for the lifetime of the process, and it makes the patterns easy to find and review.