There's more than one way to do things | |
PerlMonks |
Re: Regex - backreferencing multiple matches in one group.by moritz (Cardinal) |
on Mar 04, 2008 at 11:18 UTC ( [id://671845]=note: print w/replies, xml ) | Need Help?? |
Two ways:
1) (recommended) parse all indexes in one group, and then postprocess it (for example with split)
(capturing parenthesis on the outside) 2) "fancy": Use the experimental (?{ ... }) code assertions (but be sure to read the warnings in perlre first):
Update: here's why your solution doesn't work: The variables $1, $2, ... are set up at the time the pattern is compiled (ie before the regex engine sees the string it will match on). It counts the opening parenthesis from left to right, binding the first one to $1, the second to $2 etc. So you get this mapping:
Now each time the second group matches, it writes the captured string into $2, which means you'll get the last match of that group in $2.
In Section
Seekers of Perl Wisdom
|
|