Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

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

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

    What's my password?
    Create A New User
    talexb strolls in on this hot, hot Monday morning.

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (10)
    As of 2018-06-18 12:23 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (109 votes). Check out past polls.