⊗pyPmREMFm 64 of 128 menu

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:

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

Example

Let's apply the fullmatch method to our string:

txt = '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:

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'>

Example

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

txt = 'abcde' res = re.fullmatch('\w+', txt) print(res[0])

Result of code execution:

'abcde'

Practical tasks

Check that the following line consists only of letters:

txt = 'abcde'

Check that the following line consists only of numbers:

txt = '12345'
English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline