Q:

Python program to understand difference between match() and search() methods

belongs to collection: Python Tuple Programs

0

The match() method in Python is used to match patterns only at the beginning of the string. Whereas, the search() method is used to search patterns at any point in the string.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to illustrate the working of our solution

import re

myString = "learn python programming language at includehelp"
print("String : ",myString)

print("Matching regular expression in string using match() method.")
matchObj = re.match(r'at',myString,re.M|re.I)
if matchObj:
    print("Match Found!")
else:
    print("No Matches are found")

print("Searching regular expression in string using search() method.")
searchObj = re.search(r'at',myString,re.M|re.I)
if searchObj:
    print("Item Found!")
else:
    print("Item Not found")

Output:

String :  learn python programming language at includehelp
Matching regular expression in string using match() method.
No Matches are found
Searching regular expression in string using search() method.
Item Found!

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Tuple Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program to check the presence of substring ... >>
<< Python Regex | program to Remove leading zeros fro...