http://www.perlmonks.org?node_id=1004225

wanna_code_perl has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks,

An aspect of this regex has me stumped:

my @r; # Results, array refs for each row record $text = fetch_that_blob(); $text =~ s/ (.+?) (?:\s*\n)+ (?: (\d+ (?:\sPSI)?) (?:\s*\n)+ ){4} /push @r, [ $1, $2, $3, $4, $5 ]/esgx;

Of course, this will only return the description and last number in ($1, $2) since Perl only keeps the last successful pattern match in a quantifier.

I can cut/paste line 2 of the regexp a bunch of times to account for some arbitrary maximum number of numbers and it works, but it's sort of ugly.

Data Format

Station 1 50 PSI 59 PSI 69 PSI 74 PSI B Block 49 63 70 80 96

Note that the data may contain arbitrary newlines between lines, and whitespace at the end of lines. The numbers will sometimes include units, which will always be a literal ' PSI'. The basic pattern is (Description, number, ...).

The pattern comes into Perl as a blob.

The Question That's in Here Somewhere

I could also just split /(\s*\n)+/ or such and iterate on lines building up records as I go, but is there no way I can build @r with one regex?

I know, $me->has('cake') && $me->eat('cake'); :-)