Sometimes just modifying the input line with a very simple expression works out fine. In this case, apparently sometimes the "-", minus sign is sometimes not preceded by a space. A simple idea is to fix that in the input line (if it occurs). The code below does not modify the input line unless there is a negative number which is not preceded by a space.

Once a simple space separated token rule has been enforced, then a simple split statement suffices. I don't know if this is true in this case, but often running a couple of simple regex/split statements can execute faster than a single complicated regex.

use warnings; use strict; while (my $line = <DATA>) { chomp $line; print "Input Line: '$line'\n"; $line =~ s/(\S)-/$1 -/g; # add space before minus if needed # otherwise don't add a space print "Modified Line: '$line'\n"; my @numbers = split ' ', $line; print "Numbers are @numbers\n\n"; } =prints: Input Line: '1.234 5.6789 -1.235-4' Modified Line: '1.234 5.6789 -1.235 -4' Numbers are 1.234 5.6789 -1.235 -4 Input Line: '1.234 5.6789-12.235-4' Modified Line: '1.234 5.6789 -12.235 -4' Numbers are 1.234 5.6789 -12.235 -4 =cut __DATA__ 1.234 5.6789 -1.235-4 1.234 5.6789-12.235-4

In reply to Re: match digit, followed by hyphen, followed again by digit - then add whitespaces and replace in file by Marshall
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.