]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-44940: Clarify the documentation of re.findall() (GH-27849)
authorSerhiy Storchaka <storchaka@gmail.com>
Sun, 22 Aug 2021 07:24:20 +0000 (10:24 +0300)
committerGitHub <noreply@github.com>
Sun, 22 Aug 2021 07:24:20 +0000 (10:24 +0300)
Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com>
Co-authored-by: Vedran Čačić <vedgar+github@gmail.com>
Doc/library/re.rst

index 950012a9b0fe2f7269f3f41181dbe5a174bf2873..ff7687cc936ec9ef4c276ca38e62283c0e070b43 100644 (file)
@@ -824,10 +824,20 @@ form.
 .. function:: findall(pattern, string, flags=0)
 
    Return all non-overlapping matches of *pattern* in *string*, as a list of
-   strings.  The *string* is scanned left-to-right, and matches are returned in
-   the order found.  If one or more groups are present in the pattern, return a
-   list of groups; this will be a list of tuples if the pattern has more than
-   one group.  Empty matches are included in the result.
+   strings or tuples.  The *string* is scanned left-to-right, and matches
+   are returned in the order found.  Empty matches are included in the result.
+
+   The result depends on the number of capturing groups in the pattern.
+   If there are no groups, return a list of strings matching the whole
+   pattern.  If there is exactly one group, return a list of strings
+   matching that group.  If multiple groups are present, return a list
+   of tuples of strings matching the groups.  Non-capturing groups do not
+   affect the form of the result.
+
+      >>> re.findall(r'\bf[a-z]*', 'which foot or hand fell fastest')
+      ['foot', 'fell', 'fastest']
+      >>> re.findall(r'(\w+)=(\d+)', 'set width=20 and height=10')
+      [('width', '20'), ('height', '10')]
 
    .. versionchanged:: 3.7
       Non-empty matches can now start just after a previous empty match.