Include capture groups in `re.split()` results

Merged
LI
2mo ago
+90-4

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 mainCPython 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.py fail on main and pass on Monty and CPython 3.14. They cover several and nested groups, named and non-capturing groups, None for a group that didn't match, maxsplit, flags, Pattern.split and a natural-sort key.
  • 32,000 generated re.split() calls, with random patterns (with and without groups), subjects, maxsplit and flags, against CPython 3.14.7 and a main build. The 14,740 calls without groups give byte-identical results to main. 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 (their finditer() groups differ on main too).
  • A new test, timeout_in_re_scans, resumes re.split('(,)', s), re.split(',', s) and re.findall(',', s) on a million commas with a 10 ms limit. In a debug build without the poll, the TimeoutError came after 6.0 s, 798 ms and 740 ms; with it, after 10 to 11 ms.
  • make lint-rs, make lint-py and cargo test -p monty pass. The datatest suite passes on the Monty side, and the re cases also with memory-model-checks and ref-count-return; the 13 CPython-side cases that fail for me also fail on main in 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', '']), and re.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_iter to preserve group text; patterns without groups keep the existing Regex::split path, so their results are unchanged.
  • The new loop and the existing split() and findall() now poll the time limit via check_slice_growth, so they stop when scanning a subject with many matches.
  • maxsplit behavior 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(), and finditer().