Skip to content

[Python] Return every group from Regex.all_matches - #40563

Merged
tvalentyn merged 1 commit into
apache:masterfrom
raams95:fix-regex-all-matches-groups
Oct 14, 2026
Merged

tvalentyn merged 1 commit into
apache:masterfrom
raams95:fix-regex-all-matches-groups

Conversation

@raams95

@raams95 raams95 commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

Fixes #40562

Regex.all_matches built each output from range(m.lastindex + 1). lastindex is the index of the last group that matched, and with nested groups it is the outer group that closed last. It is None when the regex has no groups. Because of this:

p | beam.Create(['ab']) | beam.Regex.all_matches(r'((a)(b))')   # ['ab', 'ab'], groups 2 and 3 dropped
p | beam.Create(['a']) | beam.Regex.all_matches(r'(a)(b)?')     # ['a', 'a'], group 2 dropped
p | beam.Create(['aaa']) | beam.Regex.all_matches(r'a+')        # TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

Each output is now [m.group(), *m.groups()]: the entire match, then one entry per group of the regex, with None for a group that did not take part in the match. This is what Java's Regex.AllMatches returns (it adds null for such groups), and it is the same change #40399 made for Regex.find_all(..., Regex.ALL). Output for a regex whose groups all match, like the existing tests and the regex_all_matches snippet, does not change.

With this change the pipelines above print ['ab', 'ab', 'a', 'b'], ['a', 'a', None] and ['aaa'].

I added test_all_matched_returns_every_group to util_test.py. It runs the three cases in a TestPipeline. It fails with the TypeError before the change and passes after it. I also updated the docstring to describe the output.

Testing:

  • pytest apache_beam/transforms/util_test.py (157 passed, 3 skipped)
  • pytest apache_beam/examples/snippets/transforms/elementwise/ -k regex
  • yapf --diff and ruff check on the changed files

Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:

  • Mention the appropriate issue in your description (for example: addresses #123 or fixes #456).
  • Update CHANGES.md with noteworthy changes.
  • If this contribution is large, please file an Apache Individual Contributor License Agreement. (Not needed, small change.)

See the Contributor Guide for more tips on how to make review process smoother.

Regex.all_matches built its output from range(m.lastindex + 1), but
lastindex is the index of the last group that matched (or the outermost
group that closed last), not the number of groups. A regex without
groups failed with a TypeError, and nested groups or an unmatched
trailing optional group were dropped from the output. Use the entire
match followed by m.groups(), which has one entry per group, with None
for a group that did not participate, as Java's AllMatches does with
null.

Fixes apache#40562
@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @tvalentyn for label python.
Backup reviewer: damccorm (if the main reviewer is unresponsive, you can escalate to them by tagging their username)

This pull request likely touches a core component ("core" label). Please review with scrutiny.

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

@tvalentyn tvalentyn left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@tvalentyn

Copy link
Copy Markdown
Contributor

I can merge once test suites finish, please ping if i don't get back to it in a day.

@tvalentyn

tvalentyn commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

(don't worry if there are unrelated failures in the CI)

@codecov

codecov Bot commented Oct 14, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.93%. Comparing base (76f6e6d) to head (abd778f).
⚠️ Report is 177 commits behind head on master.

Additional details and impacted files
@@              Coverage Diff              @@
##             master   #40563       +/-   ##
=============================================
+ Coverage     56.16%   79.93%   +23.76%     
=============================================
  Files          1124      488      -636     
  Lines        178336    82347    -95989     
  Branches       1489        0     -1489     
=============================================
- Hits         100170    65826    -34344     
+ Misses        75645    16521    -59124     
+ Partials       2521        0     -2521     
Flag Coverage Δ
go ?
java ?
python 79.93% <100.00%> (+0.38%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@tvalentyn
tvalentyn merged commit fbc9db6 into apache:master Oct 14, 2026
86 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Regex.all_matches fails for a regex without groups and drops nested or unmatched groups

2 participants