If I were doing this for real I would use /g as Corion suggests. However, for interest's sake, here is one way to construct the pattern without repetition in the code.

#!/usr/bin/env perl use strict; use warnings; my $x = "0.01 NaN 2.30 4.44"; my $r1 = join '\s+', ('([Na0-9\.\-\+]+)') x 4; print "r1 is '$r1'\n"; my ($d, $e, $f, $g) = ($x =~ m/$r1/); print qq($d, $e, $f, $g\n);

Note that you don't need all those backslashes so the inner character class can be shortened to just [Na0-9.+-] but it has no effect on the end result.

If this is an XY Problem, then perhaps if you explain what you are actually trying to do someone could suggest a better approach.


🦛


In reply to Re: Repeating a capture group pattern within a pattern by hippo
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.