Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is the difference between Python\'s re.search and re.match?
In Python, Regular expressions (RegEx) are sequences of characters that define search patterns. The re module provides several functions to work with patterns in text for tasks like string matching, validation, and manipulation.
The most commonly used functions are re.match() and re.search(). Both check for pattern presence but differ in where they look for matches within a string.
The re.match() Method
The re.match() method checks if the beginning of a string matches the specified pattern. It returns a match object if found at the start, otherwise returns None.
Syntax
re.match(pattern, string, flags=0)
Parameters:
- pattern: The regular expression pattern to search for
- string: The string to be searched
-
flags: Optional modifiers like
re.IGNORECASE,re.MULTILINE
Example
Let's check if a string starts with "Welcome" ?
import re
text = "Welcome to Tutorialspoint"
result = re.match("Welcome", text)
if result:
print("Match found:", result.group())
else:
print("No match")
Match found: Welcome
When the pattern doesn't appear at the beginning ?
import re
text = "Welcome to Tutorialspoint"
result = re.match("Python", text)
print(result)
None
The re.search() Method
The re.search() method scans the entire string and returns the first match it finds anywhere in the string.
Syntax
re.search(pattern, string, flags=0)
The parameters are identical to re.match().
Example
Let's search for "Python" anywhere in the string ?
import re
text = "Learn Python with Tutorialspoint"
result = re.search("Python", text)
if result:
print("Match found:", result.group())
print("Position:", result.start())
else:
print("No match")
Match found: Python Position: 6
Side-by-Side Comparison
Here's a direct comparison using the same pattern and string ?
import re
text = "Hello Python world"
pattern = "Python"
# Using re.match()
match_result = re.match(pattern, text)
print("re.match() result:", match_result)
# Using re.search()
search_result = re.search(pattern, text)
print("re.search() result:", search_result.group() if search_result else None)
re.match() result: None re.search() result: Python
Key Differences
| Aspect | re.match() | re.search() |
|---|---|---|
| Search Location | Beginning of string only | Anywhere in the string |
| Returns | Match object or None | Match object or None |
| Performance | Faster (stops at start) | Slower (scans entire string) |
| Common Use Case | Format validation, prefix checking | Content searching, keyword finding |
Conclusion
Use re.match() when validating string formats or checking prefixes at the beginning. Use re.search() when looking for patterns anywhere within the string. Understanding this distinction is crucial for effective regex usage in Python.
