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.
In reply to Re: Behavior of /g when there are capture groups
by Eily
in thread Behavior of /g when there are capture groups
by ExReg
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |