|Keep It Simple, Stupid|
Capturing all instances of a repeating sub-pattern in regexby wanna_code_perl (Pilgrim)
|on Nov 16, 2012 at 17:44 UTC||Need Help??|
wanna_code_perl has asked for the
wisdom of the Perl Monks concerning the following question:
An aspect of this regex has me stumped:
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.
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'); :-)