FindAll method
Compared to other methods, the FindAll method is somewhat special. It does this by looking up all the strings in the string that can be matched, saving the results in the list, and then returning the list
Note: match and search are matched once findall match all.
1 Pattern.findall method
The function of this method is to find all the substrings that satisfy the pattern in the String[pos, Endpos] interval from the POS subscript, until the endpos position ends, and returns the result of the lookup as a list, and returns an empty list if it is not found.
Syntax format:
Pattern.findall(string[, pos[, Endpos]])
2 Re.findall
Gets all the strings in the string that can be matched and returns as a list.
Syntax format:
Re.findall(pattern, string, flags=0 )
3 when pattern has parentheses (grouping), the string in the list is only the contents of the parentheses, not the entire regular expression.(1) When a regular expression contains multiple parentheses ()
The elements in the returned list consist of all content that satisfies the match, but each element is a tuple of content that matches all the parentheses in the expression
>>> Re.findall (R ' A (b) (c) ', ' abcabc ')
[(' B ', ' C '), (' B ', ' C ')]
(2) When there is only one parenthesis in the regular expression
The elements of the returned list are surrounded by all the expressions that can be successfully matched
And the elements in the list are strings
>>> Re.findall (R ' A (b) C ', ' ABCABC ')
[' B ', ' B ']
(3) When there are no parentheses in the regular expression
The elements in the returned list are made up of all substrings that can successfully match.
>>> Re.findall (R ' ABC ', ' Abcabc ')
[' ABC ', ' ABC ']
Finditer method
The Finditer function is similar to the FindAll function, but returns an iterator instead of a list of all results, like the FindAll function.
Each object of Finditer can use group (which can get the entire matching string) and groups method;
In the case of grouping, FindAll can only get the grouping and cannot get the whole matching string.
>>> Re.findall (R ' A (b) (c) ', ' ABCD 12abcde ')
[(' B ', ' C '), (' B ', ' C ')]
>>> a = Re.finditer (R ' A (b) (c) ', ' ABCD 12abcde ')
>>> for I in a:
... print i.group ()
...
Abc
Abc
>>> a = Re.finditer (R ' A (b) (c) ', ' ABCD 12abcde ')
>>> for I in a:
... print i.groups ()
...
(' B ', ' C ')
(' B ', ' C ')
Python Regular Expressions (5)--findall, Finditer methods