Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Regex to check for very large negative numbers

by cspctec (Sexton)
on Feb 21, 2013 at 20:16 UTC ( #1019998=perlquestion: print w/replies, xml ) Need Help??
cspctec has asked for the wisdom of the Perl Monks concerning the following question:

I have a device that I'm connecting to via a perl script and outputting the numerical values it sends the script. The problem is, sometimes the device screws up and sends extremely large negative numbers to me, which it is not suppose to do.

The normal output from the device is:

-523566.00 -0.0013 0.0045

It could output numbers like the above (which are good outputs). However, sometimes it outputs garbage like this:


I'm trying to create a regular expression that will match large negative numbers and not the smaller ones. If I do something like:

my $match =~ m/-\d+\d+\d+\d+\d+\d+\d+\d+\d+\d+\d+.*\..*/;

It always matches. Can someone help me make a regular expression that matches only large negative numbers?

Replies are listed 'Best First'.
Re: Regex to check for very large negative numbers
by Old_Gray_Bear (Bishop) on Feb 21, 2013 at 20:27 UTC
    I think you are approaching this the wrong way around.
    1. Ask yourself "what is the reasonable range of expected data?" (-A <= data <= B).
    2. Now set your lower limit at 2*A and your upper limit at 2*B
    3. Validate your data by asking two questions: 1) is is less than my lower limit? 2) Is it greater than my upper limit?
    4. If the answer to either of these questions is 'yes', drop the data point. (Personally, I'd log it as well for later review.)
    Pattern Matching is a Useful Tool, just not a Universal Tool.

    Update: Cleaned up the wording. (I hate it when I change metaphors in mid stream.)

    I Go Back to Sleep, Now.


      Obviously good approach but wouldn't a regex be more appropriate in this case?

      Whether it's moritz's negative check (as the OP requested) or BillKSmith's positive approach, it seems better to avoid interpretation of a huge number that won't be needed anyway.

Re: Regex to check for very large negative numbers
by moritz (Cardinal) on Feb 21, 2013 at 20:21 UTC
Re: Regex to check for very large negative numbers
by davido (Archbishop) on Feb 22, 2013 at 00:31 UTC

    Isn't the more interesting question why your Perl script is interpreting some values in such a broken way? is there some overflow going on?


Re: Regex to check for very large negative numbers
by BillKSmith (Vicar) on Feb 21, 2013 at 23:01 UTC

    I recommend a regex that tests for a valid pattern (as defined by your device documentation) anchored at the start of the string. (e.g. /^-?\d{5,}\.\d*/)

Re: Regex to check for very large negative numbers
by Athanasius (Chancellor) on Feb 22, 2013 at 11:50 UTC

    Hello cspctec,

    You claim that the regex m/-\d+\d+\d+\d+\d+\d+\d+\d+\d+\d+\d+.*\..*/ “always matches” — but it doesn’t!

    #! perl use strict; use warnings; while (<DATA>) { chomp; print "$_ is a match\n" if /-\d+\d+\d+\d+\d+\d+\d+\d+\d+\d+\d+.*\. +.*/; } __DATA__ -523566.00 -0.0013 0.0045 -32742987982758110638106318307132432131.0000


    21:30 >perl -32742987982758110638106318307132432131.0000 is a match 21:39 >

    Perhaps the problem lies in how you’re testing the regex against the data?

    Incidentally, you can write an equivalent regex more compactly using the {n,} quantifier, which will “Match at least n times” (see “Quantifiers” in Regular Expressions):


    as per the answers above by moritz and BillKSmith.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Thanks for the help everyone. I think I have enough information to find a solution.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1019998]
Approved by Old_Gray_Bear
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2018-04-25 07:39 GMT
Find Nodes?
    Voting Booth?