104 of 151 menu

The fullmatch method of the re module

The fullmatch method of the re module searches for all matches of a regular expression in a string. The first parameter of the method specifies the regular expression to search for, and the second parameter specifies the string in which to search for it. The third optional parameter allows you to specify flags for additional regular expression settings. The method returns a Match object. If no matches are found, None is returned.

Syntax

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

Example

Let's apply the fullmatch method to our string:

txt = '123 456 789' res = re.fullmatch('\d+', txt) print(res)

After running the code, we get None, because in addition to numbers, the string also contains space characters:

None

Example

Now let's say our line consists only of numbers:

txt = '123456' res = re.fullmatch('\d+', txt) print(res)

Result of code execution:

<re.Match object; span=(0, 6), match='123456'>

See also

  • method findall of module re,
    which returns a list of all matches in a string
  • 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
  • object Match object of module re,
    which contains information about matches with a regular expression
byenru