Quickstart#
Install with uv add remagic (or pip install remagic). Python 3.11+ is required.
Build, compile, match#
Start from a building block, combine, then compile() to get an ordinary
re.Pattern.
>>> import remagic as rm
>>> year = rm.DIGIT.times(4).group("year")
>>> month = rm.DIGIT.times(2).group("month")
>>> date = year + "-" + month
>>> date
Pattern('(?P<year>\\d{4})\\-(?P<month>\\d{2})')
>>> match = date.compile().fullmatch("2027-01")
>>> match["year"], match["month"]
('2027', '01')
str(pattern) gives the regex source, and compile(flags) takes the usual
re flags.
Three rules#
Strings are literals.
"a.b"matches the texta.b, not “a, any character, b”. Everything you need is built from these pieces;remagic.unsafeexists only as a last resort.+joins,|chooses. Brackets appear only when needed.Patterns never change. Every method returns a new pattern, so a pattern can be shared and reused safely.
>>> str((rm.exactly("cat") | "dog").one_or_more())
'(?:cat|dog)+'
Where next#
Concepts explains precedence, quantifiers and the regex engines.
Cookbook has copy-paste recipes.
API reference lists every function and method.