TIOOWTDI has asked for the wisdom of the Perl Monks concerning the following question:
I stumbled over this 2 year old reddit-question from a user "onion" and am not too convinced about the answers given.
Is using Regexp::Grammars really the only way to do it? Seems like overkill...
Suppose I have this regular expression:Using the regular numeric variables only gets me the last value matched, not the whole list:my $re = qr{(\w+)(\s*\d+\s*)*}; How do I get every match matched by the second group?my $re = qr{(\w+)(\s*\d+\s*)*}; my $str = 'a 1 2 3 b 4 5 6'; while ($str =~ /$re/g) { say "$&: $1 $2"; } # output: # a 1 2 3 : a 3 # b 4 5 6: b 6How do I get every number that follows a letter in this example, and not just the last one?
EDIT
Bonus question:
How do I do it if I have named groups? I.e. my $re = qr{(?<letter>\w+)(?<digit>\s*\d+\s*)*};
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How do I reference repeated capture groups?
by kcott (Archbishop) on Aug 13, 2022 at 02:40 UTC | |
by kcott (Archbishop) on Aug 13, 2022 at 03:03 UTC | |
by Anonymous Monk on Aug 13, 2022 at 20:08 UTC | |
|
Re: How do I reference repeated capture groups?
by LanX (Saint) on Aug 12, 2022 at 14:56 UTC | |
by LanX (Saint) on Aug 12, 2022 at 19:39 UTC | |
by LanX (Saint) on Aug 13, 2022 at 21:15 UTC | |
|
Re: How do I reference repeated capture groups?
by Fletch (Bishop) on Aug 12, 2022 at 14:43 UTC | |
|
Re: How do I reference repeated capture groups?
by Anonymous Monk on Aug 13, 2022 at 06:06 UTC |