Add capture_groups to extract.regex to parse a column into its parts - #1116
Open
eastagiletracker wants to merge 1 commit into
Open
Conversation
Adds a capture_groups parameter that returns each capture group of a match as a separate result, so a single column can be parsed into one output column per group without a delimiter and a follow up split.text. Defaults to false, leaving the existing whole match behaviour unchanged. A pattern with no capture groups falls back to the whole match, and a group that did not participate in the match returns an empty string. capture_groups and output_pattern are mutually exclusive.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR proposes adding a
capture_groupsparameter toextract.regex, so a single column can be parsed into one output column per capture group (Fixes #771). We include this PR work along with a full history of your repo at https://eastagiletracker.com/projects/170. You can sign in with your GitHub ID to claim ownership of the project.extract.regexreturnsmatch.group(0)for every match, so the capture groups in a pattern are never reachable as separate results. Asking for several output columns from one input column therefore does not split a value into its parts — the extra column names are silently dropped and only the first one is written, holding the whole match. On the currentmain(12f07bb), with the sample values from #771:BoreODandWidthare never created. That is what forces the workaround in the issue: join the groups with a delimiter viaoutput_pattern, pull the first element back out, thensplit.texton the delimiter.This change adds
capture_groupstoextract.regex. When set, each match contributes its capture groups to the results instead of the whole match, and everything downstream —output_format: columns/list/concatenate,first_element, multiple input columns, thewhereclause — keeps working as it does today. Addingcapture_groups: trueto the recipe above givesModel/BoreOD/WidthofLRB/812/16, and the same run withoutput_format: listgives['LRB', '812', '16'].It is backward compatible by construction: the parameter defaults to
falseand the existing code path is untouched, so no recipe changes behavior unless it opts in. Three edges are pinned down by the design rather than left to chance — a pattern with no capture groups falls back to the whole match so results are never unexpectedly empty, a group that did not participate in a match yields an empty string (matching how the rest of the module reports a non-result), andcapture_groupstogether withoutput_patternraisesExtract must use either capture_groups or output_pattern, not both.rather than silently letting one win. The docstring schema entry is included, so the generated recipe schema picks the parameter up — confirmed by runningschema/generate_recipe_schema.pyand readingcapture_groupsback out ofextract.regex's properties.Verification: 12 tests were added to
TestExtractRegexintests/recipes/wrangles/test_extract.pycovering the three output formats, multiple matches, multiple input columns, non-string input values,first_element, the no-groups fallback, the non-participating group, the no-match case, the mutually-exclusive error, and a control asserting the default output is still the whole match. Reverting only the new branch in_matches(keeping the parameter so the recipes still load) turns 8 of the 12 red, and restoring it turns them green.pytestwas run on the clean tree first and again on the changed tree, and the set of failing tests is byte-for-byte identical (the failures are the tests needing the service credentials from the repository secrets, which are unavailable here);pytest tests/recipes/wrangles/test_extract.py::TestExtractRegexgoes from 16 passed to 28 passed.How this was managed
Issue #771 is tracked on the board as this story, moved to finished as the work landed. The board was imported from this repository's own issues and pull requests — 1079 stories and 25 labels — and used to manage this change.
If you'd rather not receive contributions like this, reply
no-more-prson this pull request and we won't open any further ones on your repositories.Lawrence W. Sinclair
CEO / East Agile
linkedin.com/in/lwsinclair/
eastagile.com