Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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'); :-)


In reply to Capturing all instances of a repeating sub-pattern in regex by wanna_code_perl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (6)
    As of 2014-07-28 21:39 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (209 votes), past polls