Python re module findall () function instance parsing, pythonfindall
This article studies the findall () function of the re module. First, let's look at the instance code:
>>> import re >>> s = "adfad asdfasdf asdfas asdfawef asd adsfas " >>> reObj1 = re.compile('((\w+)\s+\w+)') >>> reObj1.findall(s) [('adfad asdfasdf', 'adfad'), ('asdfas asdfawef', 'asdfas'), ('asd adsfas', 'asd')] >>> reObj2 = re.compile('(\w+)\s+\w+') >>> reObj2.findall(s) ['adfad', 'asdfas', 'asd'] >>> reObj3 = re.compile('\w+\s+\w+') >>> reObj3.findall(s) ['adfad asdfasdf', 'asdfas asdfawef', 'asd adsfas']
Use the preceding code example to explain:
The findall function always returns a list Of all matching results of the Regular Expression in the string. Here we mainly discuss the display method of the "result" in the list, that is, the findall function returns the information contained by each element in the list.
@ 1. when the given regular expression contains multiple parentheses, the list elements are tuple consisting of multiple strings. The number of strings in the tuple is the same as the logarithm of the parentheses, the content of the string corresponds to the Regular Expression in each bracket, and the discharge order is the order in which the brackets appear.
@ 2. when the given regular expression contains a bracket, the list element is a string. The content of this string corresponds to the regular expression in the bracket (not the Matching content of the entire regular expression ).
@ 3. When the given regular expression does not contain parentheses, the list element is a string, which is the content that the entire regular expression matches.
Summary
The above is all the content about parsing the findall () function instance of the python re module. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!