in reply to Re^4: Making regex /g match continuously
in thread Making regex /g match continuously
Option 2 as originally described doesn't work (but see Tye's response below), because when a match fails, pos gets reset, so it won't be at the end of the string. Instead, you have to set a flag by testing pos against length within the loop:
while (<DATA>) { my @accum; my $ate_the_whole_thing = 0; while (/\G$item_regex/g) { push @accum, [$1, $2, $3]; $ate_the_whole_thing = (pos() == length); } if ($ate_the_whole_thing) { print join ',', @$_, "\n" for @accum; } }
Option #3 would put a lookahead in the iterative match, which is expensive (as I noted), but does work:
while (<DATA>) { my @accum; push @accum, [$1, $2, $3] while (/\G$item_regex(?=$item_regex*\Z)/g) +; print join ',', @$_, "\n" for @accum; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Making regex /g match continuously (/c)
by tye (Sage) on Jan 08, 2005 at 07:59 UTC | |
by Roy Johnson (Monsignor) on Jan 10, 2005 at 18:37 UTC | |
|
Re^6: Making regex /g match continuously
by scottb (Scribe) on Jan 07, 2005 at 22:02 UTC |