On this page
re.match
Prev: re.fullmatch | Up: re | Next: re.Match
Rel: re.compile, re.error, re.escape, re.findall
Ref: docs.python.org — re.match
Tags: #public #python3 #stdlib #re
Import
import re
from re import match
Signature & Overview
re.match(pattern, string, flags=0)
Try to apply the pattern at the start of the string, returning a Match object, or None if no match was found.
Usage
import re
assert re.match(r"\d+", "42 apples") is not None
assert re.match(r"\d+", "apples 42") is None # only checks the start of the string!
Gotchas
- Anchored at index 0 only:
re.match()does not scan through the string—usere.search()to find a pattern anywhere, orre.fullmatch()to match the whole string. - Ignores
re.MULTILINEfor start anchoring: Even withre.MULTILINE,re.match()still only checks the beginning of the string.
REPL Inspect
from re import match
help(match)
Linked from
Related notes
- tuts/python3/stdlib_module/re/fullmatch Linked here; links to this page; shared tags: #re, #stdlib
- tuts/python3/stdlib_module/re/Match_class Linked here; links to this page; shared tags: #re, #stdlib
- tuts/python3/stdlib_module/re Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/re/compile Linked here; shared tags: #re, #stdlib
- tuts/python3/stdlib_module/re/error Linked here; shared tags: #re, #stdlib
- tuts/python3/stdlib_module/re/escape Linked here; shared tags: #re, #stdlib
- tuts/python3/stdlib_module/re/findall Linked here; shared tags: #re, #stdlib
- tuts/python3/stdlib_module/re/finditer shared tags: #re, #stdlib
- tuts/python3/stdlib_module/re/Pattern shared tags: #re, #stdlib
- tuts/python3/stdlib_module/re/PatternError shared tags: #re, #stdlib