in reply to Behavior of /g when there are capture groups
If you call the regex in scalar context (ex: as a conditional) /g will make it possible to work one match at a time, allowing you to have finer control over what you do with the captures.
#use Data::Dumper; my %res; while (/(fo.)(.*?)(ba.)/g) { $res{$&} = [ @-[1..3] ]; } print Dumper \%res;
Edit: the slice should be on indices 1..3, not 0..2.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Behavior of /g when there are capture groups
by AnomalousMonk (Archbishop) on Apr 21, 2016 at 19:18 UTC | |
by Eily (Monsignor) on Apr 21, 2016 at 22:18 UTC | |
|
Re^2: Behavior of /g when there are capture groups
by ExReg (Priest) on Apr 21, 2016 at 15:53 UTC |