Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

re.match

Attempts a match at the beginning of the string.

Version: Python 3.14; repository image, production runtime not attested

Syntax

re.match(pattern, string, flags=0)

Attempts a match at the beginning of the string.

A successful match may cover only a prefix, while a matching substring later in the input is ignored.

Use match to read a token at the beginning of the input. A Match can cover only a prefix, including an empty prefix; None means no match. MULTILINE does not move this starting position to later lines. In the example 17 is a leading token in 17kg, while the digits after x are never tried.

import re
assert re.match(r"\d+", "17kg").span() == (0, 2)
assert re.match(r"\d+", "x\n17", re.M) is None
assert re.match("a*", "z").span() == (0, 0)

Examples

import re
print(re.match(r"\d+", "17kg").group())
print(re.match(r"\d+", "x17"))

Result

17
None

Use match to read a token at the beginning of the input. A Match can cover only a prefix, including an empty prefix; None means no match. MULTILINE does not move this starting position to later lines. In the example 17 is a leading token in 17kg, while the digits after x are never tried.

Sources