damccorm commented on code in PR #40399:
URL: https://github.com/apache/beam/pull/40399#discussion_r4188391317


##########
sdks/python/apache_beam/transforms/util.py:
##########
@@ -2234,8 +2234,8 @@ def find_all(pcoll, regex, group=0, outputEmpty=True):
     def _process(element):
       matches = regex.finditer(element)
       if group == Regex.ALL:
-        yield [(m.group(), m.groups()[0]) for m in matches
-               if outputEmpty or m.groups()[0]]
+        yield [(m.group(), *m.groups()) for m in matches
+               if outputEmpty or any(m.groups() or (m.group(), ))]

Review Comment:
   Sorry for the back-and-forth here! Thinking this through more, I think we 
should actually simplify this to just gate on `if outputEmpty or m.group()` 
(matching the `group=0` behavior) and clearly document it in the docstring:
   
   ```python
         if group == Regex.ALL:
           yield [(m.group(), *m.groups()) for m in matches
                  if outputEmpty or m.group()]
   ```
   
   Otherwise, with `all` (or `any`), you can go from having a match when 
`group=0` (`outputEmpty=False`) to losing that match when `group=Regex.ALL` 
(`outputEmpty=False`) whenever an optional capturing group matches `''` (like 
`r'(\w)=(\d?)'` on `'c='` or `'a(b*)'` on `'ax'`), which is pretty surprising 
behavior.
   
   Note that this will require updating `CheckAllNonEmptyGroups` in 
`util_test.py` (since `'a'` is a non-empty full match on `'ax'`) and is a 
breaking change, so could you also add a note to `CHANGES.md` (under the 
Breaking Changes section) mentioning that `Regex.find_all` with `Regex.ALL` now 
returns all groups and checks the full match for `outputEmpty=False`?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to