How To Use Python Regex To Find All Matched String

The python regex pattern object has a findall() method which returns all matches containing the string being looked up. This is obviously different from search() method, which returns a match object containing the “first time” matched text in the string being searched. Take a look at the following examples and pay attention to the distinction.

# first you should import python regex re module. 
>>> import re 

# create a python regex pattern string. 
>>> regex_pattern_str = r'\d\d\d\d-\d\d\d\d\d\d\d\d' 

# compile the python regex pattern. 
>>> phone_num_regex = re.compile(regex_pattern_str) 

# phone numbers string. 
>>> phone_numbs_str = 'phone_num_1: 0891-88997788;phone_num_2:0281-89896868.' 

# get first time matched phone number with search method. 
>>> phone_num = phone_num_regex.search(phone_numbs_str) 

# print out the first matched phone number. 
>>> print('Matched phone number : ' + phone_num.group())
Matched phone number : 0891-88997788 

# get all matched phone number string 
>>> phone_nums = phone_num_regex.findall(phone_numbs_str)

# print out all matched phone number string. 
>>> print('Matched phone numbers : ' + str(phone_nums))
Matched phone numbers : ['0891-88997788', '0281-89896868']

If there are no groups in the python regex pattern string that calls findall() method (as in the previous example), the findall() method returns a list of matching strings ([‘0891-88997788’, ‘0281-89896868’]).

If there are groups in the python regex pattern string calling findall()method, the method findall() will return a list of string tuples (each group corresponds to a string in the tuple), as shown in the following example.

>>> import re
>>>
# there are groups in the regular expression pattern string.
>>> regex_pattern_str = r'(\d\d\d\d)-(\d\d\d\d\d\d\d\d)'
>>>
>>> phone_num_regex = re.compile(regex_pattern_str)
>>> 
>>> phone_numbs_str = 'phone_num_1: 0891-88997788;phone_num_2:0281-89896868.'
>>> 
# findall method will return groups in a tuple list.
>>> phone_nums = phone_num_regex.findall(phone_numbs_str)
>>> 
>>> print('Phone number group : ' + str(phone_nums))
Phone number group : [('0891', '88997788'), ('0281', '89896868')]
0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Clicky
0
Would love your thoughts, please comment.x
()
x