SoFunction
Updated on 2024-11-20

Python re module findall function example analysis

This article examines the re module findall() function, first look at the example code:

>>> import re 
>>> s = "adfad asdfasdf asdfas asdfawef asd adsfas " 
 
>>> reObj1 = ('((\w+)\s+\w+)') 
>>> (s) 
[('adfad asdfasdf', 'adfad'), ('asdfas asdfawef', 'asdfas'), ('asd adsfas', 'asd')] 
 
>>> reObj2 = ('(\w+)\s+\w+') 
>>> (s) 
['adfad', 'asdfas', 'asd'] 
 
>>> reObj3 = ('\w+\s+\w+') 
>>> (s) 
['adfad asdfasdf', 'asdfas asdfawef', 'asd adsfas'] 

Explained as per the above code example:

The findall function always returns a list of all the matches of a regular expression in a string, and the main discussion here is about the presentation of the "results" of the list, i.e., the information contained in each element of the findall return list.

@1. When a regular expression is given with multiple parentheses, the elements of the list are tuples of strings, the number of strings in a tuple is the same as the number of pairs of parentheses, and the content of the strings corresponds to the regular expression within each parenthesis, and the order of emission is in the order in which the parentheses appear.

@2. When a regular expression is given with a parenthesis, the elements of the list are strings whose content corresponds to the regular expression in the parenthesis (not the entire regular expression match).

@3. When the regular expression is given without parentheses, the elements of the list are strings, and this string is what the entire regular expression matches.

summarize

Above is this article on python re module findall () function example analysis of the entire content, I hope to help you. Interested friends can continue to refer to other related topics on this site, if there are inadequacies, welcome to leave a message to point out. Thank you for the support of friends on this site!