- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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 match() function in Python?
In Python, match() is a method of the module re
Syntax of match()
This 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. However, if we look for only Tutorials, the pattern will not match. Let’s check the code.
import re result = re.match(r'TP', 'TP Tutorials Point TP') print result
<_sre.SRE_Match object at 0x0000000005478648>
Above, it shows that pattern match has been found. To print the matching string we use method group. Use “r” at the start of the pattern string, it designates a python raw string.
import re result = re.match(r'TP', 'TP Tutorials Point TP') print result.group(0)
Let’s now find ‘Tutorials’ in the given string. Here we see that string is not starting with ‘TP’ so it should return no match. Let’s see what we get −
import re result = re.match(r'Tutorials', 'TP Tutorials Point TP') print result
- The match Function in Python
- What is the search() function in Python?
- What is the best test match in history of cricket?
- What is an anonymous function in Python?
- What is function of ^ operator in Python
- Significance of regex match() and regex search() function in Python
- match max_size() function in C++ STL
- What is the EqualFold function in Golang?
- What is the Function of Protocol?
- Get match indices in Python
- DI String Match in Python
- What is the function of nervous system?
- What is the function of respiratory system?