G'day fasoli,

TMTOWTDI

An array variable in a quoted (interpolated quotes) string will, by default, add spaces between the elements when interpolated. See 'perlvar: $"' for details.

$ perl -E 'my @x = (1,2,3); say ">@x<"' >1 2 3<

If you get your captures into an array, i.e. ($1,$2,$3), they too will output with spaces separating the elements.

You can do it like this:

$ perl -E 'my @x = "1.234 5.6789 -1.235" =~ /(-?\d+?\.?\d+)/g; say "@x +"' 1.234 5.6789 -1.235 $ perl -E 'my @x = "1.234 5.6789-12.235" =~ /(-?\d+?\.?\d+)/g; say "@x +"' 1.234 5.6789 -12 235

Perl v5.26 introduced some new special variables to handle this sort of thing for you (see "perldelta: @{^CAPTURE}, %{^CAPTURE}, and %{^CAPTURE_ALL}"). Using @{^CAPTURE} removes the need for an intermediate variable. Here's a couple of quick examples with your posted data:

$ perl -E '"1.234 5.6789 -1.235" =~ /^(-?\d+?\.?\d+)\s*(-?\d+?\.?\d+)\ +s*(-?\d+?\.?\d+)$/; say "@{^CAPTURE}"' 1.234 5.6789 -1.235 $ perl -E '"1.234 5.6789-12.235" =~ /^(-?\d+?\.?\d+)\s*(-?\d+?\.?\d+)\ +s*(-?\d+?\.?\d+)$/; say "@{^CAPTURE}"' 1.234 5.6789 -12.235

— Ken


In reply to Re: match digit, followed by hyphen, followed again by digit - then add whitespaces and replace in file by kcott
in thread match digit, followed by hyphen, followed again by digit - then add whitespaces and replace in file by fasoli

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.