in reply to Arbitrary number of captures in a regular expression

Take a two-step approach: First isolate the core part of the string between "foo" and "bar". Use a global match to extract all numeric sequences from the core in one more step.
while ( <DATA> ) { my ( $core) = /^foo (.*) bar$/; my @nums = $core =~ /m (\d+)/g; print "$core -> (@nums)\n"; } __DATA__ foo m 1 m 2 m 3 m 4 bar foo m 2 m 4 m 7 bar foo m 1 bar
That assumes there is only one delimiting pair of "foo" ... "bar"per string.

Anno

Replies are listed 'Best First'.
Re^2: Arbitrary number of captures in a regular expression
by bart (Canon) on Sep 24, 2007 at 09:07 UTC
    I agree on the two-step approach (at least for now 1), but I'd reverse the specificity of the regexes: I'd first do the validation using a specific regex and in a second phase split out the data you want from the capture in the first, which might be using a simpler regex because you validated the data already:
    while (<DATA>) { my ($params) = /^foo ((?:m \d+ *)*) bar$/; my @nums = $params=~ /\d+/g; print "$params -> (@nums)\n"; } __DATA__ foo m 1 m 2 m 3 m 4 bar foo m 2 m 4 m 7 bar foo m 1 bar

    1 Looking at demerphq's slides on his work on extended regexes in Perl 5.10, it might become possible to have a simpler, single step solution in the near future. See %-.

    But maybe I'm just dreaming.