re
0.1.0Regular expressions with backreferences and lookaround.
edge add re && edge lock
sha256-828e7c2ddf520235ffda8c833fff0fce579abe53ac15b0522afd8ef78da47aa4
re
re works like Python’s. match anchors at the start, search looks anywhere and fullmatch takes the whole string, and each hands back a Match or None.
<re.Match object; span=(4, 9), match='12-34'>
12-34 12 34
('12', '34') (4, 9) 7
NoneNamed groups answer by name too, and a group that took no part reads as None.
ana {'user': 'ana', 'host': 'mail'}
(None, 'b')findall gives the text of every match, or its groups when the pattern has them, and finditer gives each Match with its position.
['1', '22', '333']
[('a', '1'), ('b', '2')]
1 1
22 4sub replaces with a template, where \1 and \g<name> bring back a group, or with a function that gets each Match. count caps how many, subn also says how many it replaced, and split cuts at every match.
host.user
a2 b44
('a#b#c3', 2)
['a', 'b', 'c']compile checks a pattern once and hands back a Pattern with the same methods. The flags re.I, re.M and re.S ignore case, anchor at every line and let the dot match a newline, and escape makes any text match as written.
re.compile('hola', re.IGNORECASE) <re.Match object; span=(1, 5), match='HOLA'>
['a', 'b']
1\+1=2\?A bad pattern raises re.error, which is ValueError here, and a pattern that backtracks without end raises RuntimeError instead of hanging. bytes patterns are not supported.
bad pattern: missing ), unterminated subpattern at position 0 too slow: catastrophic backtracking: O(n^2) time or worse on this input, simplify nested quantifiers