
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
What is the difference between Python's re.search and re.match?
Both re.match() and re.search() are methods of the Python module re.
The re.match() method finds match if it occurs at start of the string. For example, calling match() on the string ‘TP Tutorials Point TP’ and looking for a pattern ‘TP’ will match.
example
import re result = re.match(r'TP', 'TP Tutorials Point TP') print result.group(0)
Output
TP
The re.search() method is similar to re.match() but it doesn’t limit us to find matches at the beginning of the string only.
example
import re result = re.search(r'Tutorials', 'TP Tutorials Point TP') print result.group(0)
Output
Tutorials
Here you can see that, search() method is able to find a pattern from any position of the string.
- Related Articles
- Comparison between Regression Testing and Re-Testing
- What is meant by re-throwing exceptions in Java?
- Explain subn() methods of “re” module in Python ?
- Explain the Java regular expression construct "re?".
- Removing Array Element and Re-Indexing in PHP
- Regular Expression "re*" Metacharacter in Java
- Regular Expression re+ Metacharacter in Java
- What is the difference between a search engine friendly and search engine optimised website?
- What happens if we re-declare a variable in JavaScript?
- Regular Expression re{ n} Metacharacter in Java
- Regular Expression "(re)" Sub-Expression in Java
- Sub-Expression "(?: re)" in Java Regular Expressions
- What is the difference between –Match, -Like and –Contains Operator in PowerShell?
- What's the difference between lists and tuples in Python?
- Explain the Sub-Expression "(?> re)" in Java Regular Expressions

Advertisements