Open In App

re.search() vs re.match() – Python

Last Updated : 08 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

When working with regular expressions (regex) in Python, re.search() and re.match() are two commonly used methods for pattern matching. Both are part of the re module but function differently. The key difference is that re.match() checks for a match only at the beginning of the string, while re.search() searches the entire string for the pattern. Example:

Python
import re
s = '''We are learning regex with geeksforgeeks 
    regex is very useful for string matching.'''

a = re.match(r"regex", s)
print(a)

b = re.search(r"regex", s)
print(b)

Output
None
<re.Match object; span=(16, 21), match='regex'>

Explanation:

  • re.match() searches for “regex” only at the beginning of the string. Since it’s not at the start, it returns None.
  • re.search() searches for “regex” anywhere in the string. It finds it at index 16 and returns a match object.

When to use what?

Both re.match() and re.search() are powerful tools for pattern matching in strings. However, knowing when to use each can significantly improve both efficiency and accuracy in your code. Let’s dive into understanding when to use re.match() and re.search().

1. Use re.match(): When you’re only interested in checking if a string begins with a specific pattern, you can use re.match(). This is especially useful for validating scenarios like file formats, prefixes or any fixed starting patterns in strings. For example, checking if a string starts with a particular number, letter, or keyword.

Example 1: Checking if a string starts with a number

Python
import re
res = re.match(r"\d{4}", "2025 is the year")  # Matches "2025"
print(res)

Output
<re.Match object; span=(0, 4), match='2025'>

Explanation: re.match(r”\d{4}”, “2025 is the year”) checks for four digits at the start of the string and since “2025” appears right at the beginning, it returns a match object.

Example 2: Matching a string that starts with a capital letter

Python
import re
res= re.match(r"^[A-Z]", "Hello world")  # Matches
print(res)

Output
<re.Match object; span=(0, 1), match='H'>

Explanation: re.match(r”^[A-Z]”, “Hello world”) checks if the string starts with an uppercase letter from A to Z and since “H” is uppercase at the beginning, it returns a match object.

2. Use re.search(): when the pattern can appear anywhere in the string. It’s ideal for searching, filtering or scanning large texts like logs, documents or datasets.

Example 1: Searching for a keyword anywhere in the text

Python
import re
res = re.search(r"error", "Server error at 5:00 PM")  # Matches "error"
print(res)

Output
<re.Match object; span=(7, 12), match='error'>

Explanation: re.search(r”error”, “Server error at 5:00 PM”) looks for the substring “error” anywhere in the text and since it appears after “Server”, it finds a match and returns a match object.

Example 2: Finding the first occurrence of ‘apple’ anywhere in the string.

Python
import re
res = re.search(r"apple", "I have an apple and an orange.")
print(res) 

Output
<re.Match object; span=(10, 15), match='apple'>

Explanation: re.search(r”apple”, “I have an apple and an orange.”) searches for the first occurrence of the substring “apple” in the string. Since “apple” appears in the text, it finds a match and returns a match object.

Difference between re.search() vs re.match()

In Python regex, re.match() and re.search() both find patterns, but differ in where they look. The table below highlights their key differences to help you choose the right one.

Feature

re.match()

re.search()

Search Location

Only at the beginning of the string.

Anywhere in the string.

Return Value

Match object if at the start; otherwise None.

Match object for first match; otherwise None.

Use Case

Match pattern at the start.

Find pattern anywhere in the string.

Example

re.match(r”hello”, “hello world”) (matches)

re.search(r”world”, “hello world”) (matches)

Behavior

if No Match Returns None if no match at the start.

Returns None if no match anywhere.

Performance

Faster for start-of-string matching.

More flexible but may be slower.



Article Tags :
Practice Tags :

Similar Reads

  翻译: