Include capture groups in `re.split()` results
re.split() dropped the text of capture groups.
CPython puts it in the result after the piece before each match, with None for a group that didn't take part:
Monty main | CPython 3.14 | |
|---|---|---|
re.split(r'(,)', 'a,b,c') | ['a', 'b', 'c'] | ['a', ',', 'b', ',', 'c'] |
re.split(r'(a)|b', 'xaybz') | ['x', 'y', 'z'] | ['x', 'a', 'y', None, 'z'] |
re.split(r'([.!?])\s*', 'Hi. How are you?') | ['Hi', 'How are you', ''] | ['Hi', '.', 'How are you', '?', ''] |
The usual natural-sort key, [int(t) if t.isdigit() else t for t in re.split(r'(\d+)', s)], loses the numbers, so sorted(['img12.png', 'img2.png', 'img1.png'], key=natural_key) leaves the list as it was.
This isn't in limitations/re.md.
RePattern::split now walks captures_iter when the pattern has groups and adds each group's text, or None, after the piece before the match, with maxsplit handled as before.
Patterns without groups keep the Regex::split path, so their results don't change.
The new buffer is preflighted with check_slice_growth and the list with check_allocation, like the existing split.
check_slice_growth now also polls the time limit with check_time_every, so this loop, the existing split and findall() stop at the limit.
Testing
- New asserts in
re__grouping.pyfail onmainand pass on Monty and CPython 3.14. They cover several and nested groups, named and non-capturing groups,Nonefor a group that didn't match,maxsplit, flags,Pattern.splitand a natural-sort key. - 32,000 generated
re.split()calls, with random patterns (with and without groups), subjects,maxsplitand flags, against CPython 3.14.7 and amainbuild. The 14,740 calls without groups give byte-identical results tomain. Of the 25,739 calls where no empty match is involved, all but 135 match CPython, and those 135 come from the second engine difference below (theirfinditer()groups differ onmaintoo). - A new test,
timeout_in_re_scans, resumesre.split('(,)', s),re.split(',', s)andre.findall(',', s)on a million commas with a 10 ms limit. In a debug build without the poll, theTimeoutErrorcame after 6.0 s, 798 ms and 740 ms; with it, after 10 to 11 ms. make lint-rs,make lint-pyandcargo test -p montypass. The datatest suite passes on the Monty side, and therecases also withmemory-model-checksandref-count-return; the 13 CPython-side cases that fail for me also fail onmainin my environment.
Two engine differences remain, the same as on main and in findall() and finditer():
- An empty match right after another match is skipped:
re.split(r'x*', 'axbc')is['', 'a', 'b', 'c', ''](CPython['', 'a', '', 'b', 'c', '']), andre.findall(r'\w*', 'ab c')differs the same way. - A repeated group whose last iteration matches empty keeps the earlier capture:
re.match(r'(a?)*', 'aa').group(1)is'a'(CPython'').
Neither is in limitations/re.md yet. I can add them there, or look at the first one separately.
Summary by cubic
Fixes re.split() dropping capture-group text, so split results now include each group's matched text (or None for a group that didn't take part) right after the piece before the match, matching CPython.
- Patterns with groups now use
captures_iterto preserve group text; patterns without groups keep the existingRegex::splitpath, so their results are unchanged. - The new loop and the existing
split()andfindall()now poll the time limit viacheck_slice_growth, so they stop when scanning a subject with many matches. maxsplitbehavior is unchanged: positive values limit splits, negative returns the whole subject.- Two pre-existing engine differences with CPython (empty match after another match, and repeated group captures) remain and also affect
split(),findall(), andfinditer().