in reply to Regex to extract multiple occurrences

ikegami provided a very good answer for why your original regexp didn't return the captures you were hoping for, and he provided a good m//g solution. But I wanted to point out yet another way to do it, using the experimental (?{...code...}) construct, and as you requested, a single regular expression. ;)

use strict; use warnings; use vars qw/@array/; $_ = "This is list number 12. It contains apples, pears, peaches. Tota +l cost is 5."; if ( m/contains\s+ (?:(\w+)(?{push @array, $^N}),\s+)* (?:(\w+)(?{push @array, $^N})\.\s+\b) /x ) { print "@array\n"; }

I used the /x modifier to break the regexp into smaller chunks so you could see more clearly what's going on. The special variable $^N contains the most recent capture within the regexp. So even though $1 only contains the last match in the first set of parens, if you check at the proper point in time, it (and $^N) will contain that first capture too. Be sure to read up in perlre for details. This is a tricky subject, but kind of fun, IMHO.


Dave