re.match(pattern, string, flags=0)
The re.match function checks if the regular expression pattern is satisfied at the beginning of the text string, returning the corresponding match object if it is positive. In case of no match, the function returns None.
- pattern: Search pattern.
- string: Text in which to search.
- flags: Search modifiers.
The re.search function returns an object of type match or None if the match is not found at the beginning of the text string.
We can check if the texts "my cat" or "my cats" are found at the beginning of several sentences with the following code:
re.match(pattern, text)
re.match(pattern, text)
In this next example the regular expression is satisfied ("my cats"), but not at the beginning of the text, so the re.match function returns None (which is why nothing is displayed on the screen).
re.match(pattern, text)