100 of 151 menu

The findall method of re module

The findall method of the re module returns a list of all matches with a regular expression. If the regular expressions are placed in pockets, the method will return a tuple. In the first parameter of the method, we specify the regular expression to search for, and in the second parameter, the string to search for. In the third optional parameter, you can specify flags for additional regular expression settings. The method checks all matches, searching for them from left to right.

Syntax

import re re.findall(regular expression, string, [flags bunting])

Example

Let's find all matches with a regular expression in a string:

txt = '12 43 56 ab' res = re.findall('\d+', txt) print(res)

Result of code execution:

['12', '43', '56']

Example

Now let's find all the matches in the regular season pockets:

txt = '12 43 56 ab' res = re.findall('(\d)(\d)', txt) print(res)

Result of code execution:

[('1', '2'), ('4', '3'), ('5', '6')]

See also

  • method finditer of module re,
    which returns an iterator of all matches of the regular expression in the string
  • method search of module re,
    which looks for the first match of a regular expression in a string
  • method match of module re,
    which looks for a match with a regular expression at the beginning of a line
  • method fullmatch of module re,
    which looks for all matches of a regular expression in a string
  • method split of module re,
    which splits a string into a list by the specified separator
byenru