Skip to content

Commit 64f9e7b

Browse files
serhiy-storchakaFidget-Spinnervedgar
authored
bpo-44940: Clarify the documentation of re.findall() (GH-27849)
Co-authored-by: Ken Jin <[email protected]> Co-authored-by: Vedran Čačić <[email protected]>
1 parent 585390f commit 64f9e7b

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

Doc/library/re.rst

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -824,10 +824,20 @@ form.
824824
.. function:: findall(pattern, string, flags=0)
825825

826826
Return all non-overlapping matches of *pattern* in *string*, as a list of
827-
strings. The *string* is scanned left-to-right, and matches are returned in
828-
the order found. If one or more groups are present in the pattern, return a
829-
list of groups; this will be a list of tuples if the pattern has more than
830-
one group. Empty matches are included in the result.
827+
strings or tuples. The *string* is scanned left-to-right, and matches
828+
are returned in the order found. Empty matches are included in the result.
829+
830+
The result depends on the number of capturing groups in the pattern.
831+
If there are no groups, return a list of strings matching the whole
832+
pattern. If there is exactly one group, return a list of strings
833+
matching that group. If multiple groups are present, return a list
834+
of tuples of strings matching the groups. Non-capturing groups do not
835+
affect the form of the result.
836+
837+
>>> re.findall(r'\bf[a-z]*', 'which foot or hand fell fastest')
838+
['foot', 'fell', 'fastest']
839+
>>> re.findall(r'(\w+)=(\d+)', 'set width=20 and height=10')
840+
[('width', '20'), ('height', '10')]
831841

832842
.. versionchanged:: 3.7
833843
Non-empty matches can now start just after a previous empty match.

0 commit comments

Comments
 (0)