The OR operator in Python’s re module — represented by the vertical bar | — allows a regular expression to match either one pattern or another (or more) patterns. It’s the regex equivalent of logical OR, enabling flexible alternatives in matching — for example, matching different keywords, formats, or structures in text. In 2026, the OR operator remains one of the most essential regex features — used constantly in data validation (e.g., multiple date formats), text extraction (different log levels), search (synonyms or variants), and vectorized pandas/Polars string operations where pattern alternatives scale efficiently across large datasets.
Here’s a complete, practical guide to the OR operator in Python regex: basic usage, grouping for complex alternatives, precedence and scope, real-world patterns, and modern best practices with raw strings, flags, compilation, and pandas/Polars integration.
The | operator matches the pattern on its left OR the pattern on its right — it has low precedence, so alternatives often need parentheses to scope them correctly.
import re
text = "The quick brown fox jumps over the lazy dog."
# Simple OR — match "quick" OR "lazy"
pattern = r"quick|lazy"
matches = re.findall(pattern, text)
print(matches) # ['quick', 'lazy']
# Without parentheses — OR has low precedence
print(re.findall(r"quick brown|lazy dog", text)) # ['quick brown', 'lazy dog']
# With parentheses — scope the OR correctly
print(re.findall(r"(quick brown)|(lazy dog)", text)) # [('quick brown', ''), ('', 'lazy dog')]
Grouping alternatives with parentheses — allows OR to apply to multi-character patterns or subexpressions.
# Match "cat" OR "dog" OR "fox"
animals = re.findall(r"cat|dog|fox", "The cat and dog chased the fox")
print(animals) # ['cat', 'dog', 'fox']
# Match different greetings
greetings = re.findall(r"(hello|hi|hey)\s+world", "hello world, hi world, hey world!")
print(greetings) # ['hello', 'hi', 'hey']
Real-world pattern: flexible pattern matching in pandas — vectorized .str.contains() or .str.extract() uses OR for multiple formats efficiently.
import pandas as pd
df = pd.DataFrame({
'log': [
"ERROR: connection failed",
"INFO: data loaded",
"WARNING: low memory",
"DEBUG: debug message"
]
})
# Match any error/warning level
df['is_alert'] = df['log'].str.contains(r"^(ERROR|WARNING)", regex=True)
df['level'] = df['log'].str.extract(r"^(ERROR|WARNING|INFO|DEBUG)")
print(df)
# log is_alert level
# 0 ERROR: connection failed True ERROR
# 1 INFO: data loaded False INFO
# 2 WARNING: low memory True WARNING
# 3 DEBUG: debug message False DEBUG
Best practices make OR operator usage safe, readable, and performant. Always group alternatives with parentheses when they contain more than one character — (cat|dog) vs cat|dog prevents unintended matching. Use raw strings r'pattern' — avoids double-escaping backslashes. Compile patterns with re.compile() for repeated use — faster and clearer. Use flags like re.IGNORECASE — pass as argument or via compiled pattern. Modern tip: use Polars for large text columns — pl.col("text").str.contains(r"(ERROR|WARNING)") is 10–100× faster than pandas .str.contains(). Add type hints — str or pd.Series[str] — improves static analysis. Order alternatives from longest/most specific to shortest — cat|ca matches "cat" first. Avoid overusing OR — simple string methods (split(), replace()) are faster when sufficient. Combine with pandas.str — df['col'].str.extract(r"(pattern1|pattern2)") for vectorized extraction. Use re.escape() for literal substrings in patterns.
The OR operator (|) gives regex the power of alternatives — match one pattern OR another, with grouping for complex choices. In 2026, group alternatives, use raw strings, compile patterns, vectorize in pandas/Polars, and order from specific to general. Master OR, and you’ll build flexible, efficient pattern matching for validation, extraction, and text processing.
Next time you need to match one thing OR another — use the OR operator. It’s Python’s cleanest way to say: “Match this pattern or that one.”