Last modified: Nov 19, 2026
Python Match vs Search in Regex
Regex is a powerful tool in Python. It helps you find patterns inside text. But two functions often confuse beginners: re.match() and re.search().
They look similar. They behave differently. This guide explains both clearly, with code and output.
What Is Regex in Python?
Regex stands for regular expression. It is a pattern used to match text. Python ships with the built-in re module for this job.
You can use it to validate emails, extract numbers, or split strings. If you are new to the module, start with this complete guide to the Python re module.
The two most common functions are re.match() and re.search(). Let's break them down.
How re.match() Works
re.match() checks for a match only at the beginning of the string. If the pattern is not at the start, it returns None.
Think of it as a strict checker. The pattern must sit at position zero.
import re
text = "Python is fun"
# Pattern is at the start, so match works
result = re.match(r"Python", text)
print(result)
# Pattern is not at the start, so match returns None
result2 = re.match(r"fun", text)
print(result2)
None
Notice the second call returned None. The word "fun" exists in the string, but not at the start.
How re.search() Works
re.search() scans the whole string. It returns the first match it finds anywhere.
Think of it as a flexible finder. It keeps looking until it finds something.
import re
text = "Python is fun"
# search finds the pattern anywhere in the string
result = re.search(r"fun", text)
print(result)
# search also works when the pattern is at the start
result2 = re.search(r"Python", text)
print(result2)
Both calls succeeded. That is the key difference between match and search.
Match vs Search: Side by Side
Here is a direct comparison using the same string and pattern.
import re
text = "hello world"
# match checks only the start
print(re.match(r"world", text)) # None
# search checks the whole string
print(re.search(r"world", text)) # Match found
None
So match is anchored to the start. Search is not anchored at all.
Using Anchors with Both Functions
You can force re.search() to behave like match by using the ^ anchor. The caret means "start of string".
import re
text = "hello world"
# ^ anchors the pattern to the start
print(re.search(r"^world", text)) # None
print(re.search(r"^hello", text)) # Match
None
This trick is useful when you want one function for everything. Many developers prefer search with anchors for clarity.
Working with Match Objects
Both functions return a match object or None. A match object holds useful data.
You can read the matched text with group(). You can also get positions with span().
import re
result = re.search(r"\d+", "Order 42 shipped")
if result:
print(result.group()) # matched text
print(result.start()) # start index
print(result.end()) # end index
42
6
8
Always check for None before calling these methods. Otherwise your program will crash with an AttributeError.
Flags and Performance Tips
Both functions accept a flags argument. For example, re.IGNORECASE makes matching case-insensitive.
import re
# Case-insensitive search
print(re.search(r"python", "I love PYTHON", re.IGNORECASE))
If you reuse the same pattern many times, compile it once. This saves time. Learn how in this guide on how to compile a regex pattern in Python.
Need every match, not just the first? Use findall instead. See this tutorial on finding all matches with re.findall.
Common Mistakes to Avoid
Beginners often expect re.match() to behave like search. It does not. That is the number one mistake.
Another common error is forgetting the raw string prefix. Always write patterns as r"pattern". This prevents escape character bugs.
Also, avoid calling .group() on a result without checking for None first.
When Should You Use Each?
Use re.match() when the pattern must appear at the start. Good examples are prefixes, log line types, or command names.
Use re.search() when the pattern can appear anywhere. This covers most real-world cases like finding a number or a keyword.
When in doubt, choose search. It is more flexible and rarely surprises you.
Conclusion
The difference between match and search is simple. Match checks the start only. Search checks the whole string.
Both return a match object or None. Both accept flags. Both are fast when used well.
Practice with short examples first. Then try them on real text. With a little use, the choice between match and search becomes automatic.