in reply to regexp match repetition breaks in Perl
A match operation will always return exactly as many values as there are captures, so /(...)(?:(...))*(...)/ will always return exactly three results on a match. When using the g modifier, the match operation will always return an exact multiple of the number of captures. You need a parser.
Here's a simple solution:
while ($text =~ m/ (APC[s]?) ( \s \d{3} (?: (?: , \s \d{3} )* \s and \s \d{3} )? ) /xg) { my ($apc, $nums) = ($1, $2); my @nums = $nums =~ /(\d+)/g; push @extracts, $apc, @nums; }
By the way, what's with pos($text) = 0 and the c switch? Removed!
Update: Added a solution.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: regexp match repetition breaks in Perl
by barkingdoggy (Initiate) on Jul 11, 2007 at 23:14 UTC | |
by ikegami (Patriarch) on Jul 12, 2007 at 01:02 UTC |