- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 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 re.match(), re.search() and re.findall() methods in Python?
re.match(), re.search(), and re.findall() are methods of the Python module re (Python Regular Expressions).
The re.match() method
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
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
The re.findall() method
The re.findall() helps to get a list of all matching patterns. It searches from start or end of the given string. If we use method findall to search for a pattern in a given string it will return all occurrences of the pattern. While searching a pattern, it is recommended to use re.findall() always, it works like re.search() and re.match() both.
Example
import re result = re.search(r'TP', 'TP Tutorials Point TP') print result.group()
Output
TP