
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the difference between Python\\\\\'s re.search and re.match?
In Python, Regular expressions are also called RegEx, which is a sequence of characters that defines a search pattern. The re module provides several functions to work with patterns in text. This is widely used in Python tasks such as string matching, validation, and manipulation.
The most commonly used functions in the "re" module are re.match() and re.search(). These two methods are used to check for the presence of a pattern, but they differ in the way they look for a pattern match within a string.
In this article, we are going to see the difference between the Python re.search() and re.match() methods.
The re.match() Method
The re.match() method is used to check if the beginning of the given string matches the specified pattern or not. It returns a match object if a match is found at the start of the string, otherwise, it returns None.
Syntax
Following is the syntax of the method re.match() in Python -
re.match(pattern, string, flags=0)
Where,
- pattern: The regular expression pattern to search for.
- string: The string to be searched.
-
flags: Optional modifiers like
re.IGNORECASE
,re.MULTILINE
, etc.
Example
Following is the example, in which we are going to check if the given string matches the defined pattern "Welcome" using the re.match() method -
import re text = "Welcome to Tutorialspoint" result = re.match("Welcome", text) if result: print("Match found:", result.group()) else: print("No match")
Here is the output of the above program -
Match found: Welcome
Now, if the pattern does not appear at the beginning of the input string, then the code and output will be as follows -
import re text = "Welcome to Tutorialspoint" result = re.match("Python", text) print(result)
Following is the output -
None
The re.search() Method
The re.search() method scans the entire string and returns the first match it finds. It does not require any pattern to be at the beginning.
Syntax
Following is the syntax of the method re.search() in Python -
re.search(pattern, string, flags=0)
Where,
- pattern: The regular expression pattern to search for.
- string: The string to be searched.
-
flags: Optional modifiers like
re.IGNORECASE
,re.MULTILINE
, etc.
Example
Here is the example, in which we are going to search the defined pattern "Python" throughout the input text with the help of the re.search() method -
import re text = "Python with Tutorialspoint" result = re.search("Python", text) if result: print("Match found:", result.group()) else: print("No match")
Here is the output of the above program -
Match found: Python
Difference between re.search() and re.match()
Following are the key differences between the two methods re.match() and re.search() -
Aspect | re.match() | re.search() |
---|---|---|
Match Location | Beginning of string only | Anywhere in the string |
Returns | Match object or None | Match object or None |
Typical Use Case | Validating format, such as starting with the prefix | Searching for keywords or patterns in content |
It is necessary to understand the difference between re.match() and re.search() methods when working with regular expressions in Python.
If we want to test the start of a string, then we have to use the re.match() method, and when we're looking for a pattern anywhere in the string, then we have to use the re.search() method.