Python re findall()














































Python re findall()



findall() function

re module provides us with the function findall() that is used to find all the match present in the given string. As an output it returns a list containing all the matches in the form of string. However, it does not tell anything about the position of the occurrences of the match. If the pattern specified has groups then the output will be a list of tuples instead of strings.

Syntax:

re.findall(“<pattern>”,”<string>”, [flag=0])

 (Things written inside ‘< >’ are compulsory but things written under’[ ]’ are optional)

For some examples of findall function you can refer my previous articles, link for them are as follows:

re Special Sequence

re Sets

Code :


Here you can see that the pattern specified is “[w.-]+@[w.-]+”. [w.-]+@ will try extracting the characters which are like “xyz@”, i.e., the initial part of the email ids.


You may note that initial part of email id is extracted only because of the presence of ‘@’ if you skip writing ‘@’ then the pattern will be extracting every letter of the text.

Whereas “@[w.-]+” will extract the end part of the email as seen below.


 

 


Comments