"There is more than one way to do it" ™ depending on your use case.

The "problem" is not that you can't repeat a pattern in Perl, but that only the last captures are kept for explicit (...) groups.

One way is a code section to store the current capture groups.

Another to create explicit captures.

DB<25> $_='1016.1 7.7 NaN -20.6 3.8 72.9 215.0' DB<26> $pat = qr(NaN|-?\d+\.\d) DB<27> x m/($pat)/g 0 1016.1 1 7.7 2 'NaN' 3 '-20.6' 4 3.8 5 72.9 6 215.0 DB<28> x m/($pat)(?:\s|$)/g 0 1016.1 1 7.7 2 'NaN' 3 '-20.6' 4 3.8 5 72.9 6 215.0 DB<29> x (m/($pat)(?:\s|$)/g)[0..3] 0 1016.1 1 7.7 2 'NaN' 3 '-20.6' ... DB<33> x m/(?:($pat)(?:\s|$)){4}/ 0 '-20.6' DB<34> x m/(?:($pat)(?:\s|$)(?{push @a,$1})){4}/ 0 '-20.6' DB<35> x @a 0 1016.1 1 7.7 2 'NaN' 3 '-20.6' DB<36> ... DB<47> $delim = '(?:\s|$)' DB<48> p $explicit= "($pat)$delim" x 4 ((?^u:NaN|-?\d+\.\d))(?:\s|$)((?^u:NaN|-?\d+\.\d))(?:\s|$)((?^u:NaN|-? +\d+\.\d))(?:\s|$)((?^u:NaN|-?\d+\.\d))(?:\s|$) DB<49> x m/$explicit/g 0 1016.1 1 7.7 2 'NaN' 3 '-20.6' DB<50>

Cheers Rolf
(addicted to the Perl Programming Language :)
see Wikisyntax for the Monastery


In reply to Re: Repeating a capture group pattern within a pattern by LanX
in thread Repeating a capture group pattern within a pattern by mldvx4

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!
  • 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, details, 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, summary, 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.