Jun-06-2021, 09:07 AM
(Jun-06-2021, 05:55 AM)Gribouillis Wrote: I don't think you can capture a variable number of groups in the same match. Each captured group correspond to a single group in the regex. For example the regexWhile you might be technically correct, I expect python to do better than that!r"key_phrase \[(?:(?: )*(\d+))+\]"
has visibly a single capturing group(\d+)
hence there will be only a single group in the match.
r"key_phrase \[|(?: )*(\d+)|\]"
has the same single visible capture group, and while in that brief form does include multiple matches, it also returns multiple instances of Group 1
; and relies on the proper placement of the two |
"alternative" designators to return ALL the desired captures.Along side the fact that the expression
r"key_phrase \[((?:(?: )*(?:\d+))+)\]"
does eliminate the extra matches, while failing to separate out the individual \d+
instances … it seems to me, that if I REALLY knew what I was doing, these two expression contain most of the clues on how to proceed to a simpler | cleaner | more pythonic expression that does achieve the desired goal.I am somewhat clueless as to how acceptable, pythonic speaking, multiple instances of the same group are? vs the incrementing group number that would result if one took the time to type in some thousand (or so) optional-capture-group-sub-expressions? And I still expect that python should have a short elegant way of coding an expression to make multiple captures in such situations.