The fullmatch method in Python regular expressions

The fullmatch method checks a string for a full match with a regular expression. In the first parameter of the method, we specify the regular expression we will search for, in the second parameter - the string in which we will search for it. If a match is found, the method will return a match object, otherwise - None. The syntax of the fullmatch method looks like this:

pythonre.fullmatch(what to look for, where to look)

Example

Let's apply the fullmatch method to our string:

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

print(res)

After executing the code, None will be displayed, because in addition to numbers, the string also contains space characters:

pythonNone

Example

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

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

print(res)

Result of code execution:

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

Example

You can get the found substring from the match object by accessing its first element:

pythontxt = 'abcde'
res = re.fullmatch('\w+', txt)

print(res[0])

Result of code execution:

python'abcde'

Practical tasks

Задание 1

Check that the following line consists only of letters:

pythontxt = 'abcde'
Задание 2

Check that the following line consists only of numbers:

pythontxt = '12345'