Explain method of regular expression in Python

OR Explain Python RegEx


A RegEx, or Regular Expression, is a sequence of characters that forms a search pattern.
RegEx can be used to check if a string contains the specified search pattern. First you have to Import the re module: i​ mport re
RegEx Functions(Method):
The re module offers a set of functions that allows us to search a string for a match:
FunctionDescription
findallReturns a list containing all matches
searchReturns a Match object if there is a match anywhere in the string
splitReturns a list where the string has been split at each match
subReplaces one or many matches with a string
Program of re.findall():

import re string = 'hello 12 hi 89. Howdy 34' 
pattern = '\d+'
result = re.findall(pattern, string) 
print(result)

Output: ['12', '89', '34']

To Learn more about Python, Click Here

Click here To Follow on pinterest

Leave a Comment

Your email address will not be published. Required fields are marked *