in reply to capturing multiple repeated regex subparts

Regexps are useful for validation, extraction and tokenizing. However, they are not as strong at parsing, as you have discovered. Parsing is nontheless possible, using advanced features.

use v5.8.0; # or higher # For $^N our @rv; our @temp_rv; / (text); (?{ local @temp_rv = ( @temp_rv, $^N ) }) (?: (float) (?{ local @temp_rv = ( @temp_rv, $^N ) }) (?:non-num) ){4} (?{ @rv = @temp_rv }) /x;

Tested.

Replies are listed 'Best First'.
Re^2: capturing multiple repeated regex subparts
by japhy (Canon) on Feb 17, 2006 at 21:18 UTC
    Don't eschew $^R! That's what it's there for:
    # UPDATED: added comments about what's going on our @rv; # you like taking trips? ;) m{ (text); # the (?{ ... }) block's return value # is given to $^R, the magical variable # whose value is auto-localized and gets # rolled back when backtracking occurs. # $^R's initial value, then, is an array # ref with one element, $1's value. (?{ [$1] }) (?: (float) # then, four times, we add the float we # match in $2 to the end of $^R. we # can't just do push(@{$^R}, $2), because # that would break the auto-rollback magic, # so instead, we just let the return value # set $^R again. (?{ [ @{$^R}, $2 ] }) non-num ){4} # finally, we store @{$^R} in @rv. (?{ @rv = @{$^R} }) }x;

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart