re.Pattern
Prev: re.Match | Up: re | Next: re.PatternError
Rel: re.compile, re.error, re.escape, re.findall
Ref: docs.python.org — re.Pattern
Tags: #public #python3 #stdlib #re
Import
import re
from re import Pattern
Signature & Overview
re.Pattern()
Compiled regular expression object.
Methods & Attributes
Pattern.findall(self, /, string, pos=0, endpos=9223372036854775807)— Return a list of all non-overlapping matches of pattern in string.Pattern.finditer(self, /, string, pos=0, endpos=9223372036854775807)— Return an iterator over all non-overlapping matches for the RE pattern in string.Pattern.fullmatch(self, /, string, pos=0, endpos=9223372036854775807)— Matches against all of the string.Pattern.match(self, /, string, pos=0, endpos=9223372036854775807)— Matches zero or more characters at the beginning of the string.Pattern.scanner(self, /, string, pos=0, endpos=9223372036854775807)—Pattern.scannermethod/property.Pattern.search(self, /, string, pos=0, endpos=9223372036854775807)— Scan through string looking for a match, and return a corresponding match object instance.Pattern.split(self, /, string, maxsplit=0)— Split string by the occurrences of pattern.Pattern.sub(self, /, repl, string, count=0)— Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl.Pattern.subn(self, /, repl, string, count=0)— Return the tuple (new_string, number_of_subs_made) found by replacing the leftmost non-overlapping occurrences of pattern with the replacement repl.
Usage
import re
pat = re.compile(r"\d{4}-\d{2}-\d{2}")
assert isinstance(pat, re.Pattern)
assert pat.findall("dates: 2026-01-01 and 2026-12-02") == ["2026-01-01", "2026-12-02"]
assert pat.pattern == r"\d{4}-\d{2}-\d{2}"
Gotchas
- Create via
re.compile():re.Patterncannot be instantiated directly. - Supports
posandendposslicing:pat.search(text, pos, endpos)searches a slice without copying the string (unlike module-levelre.search()).
REPL Inspect
from re import Pattern
help(Pattern)
Linked from
Related notes
- tuts/python3/stdlib_module/re/Match_class Linked here; links to this page; shared tags: #re, #stdlib
- tuts/python3/stdlib_module/re/PatternError 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/fullmatch shared tags: #re, #stdlib
- tuts/python3/stdlib_module/re/match shared tags: #re, #stdlib