Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx that matches positive numbers

Tags:

regex

I need to write a regex to allow only positive number ( integers or decimals). I have found this:

/^(?!(?:0|0\.0|0\.00)$)[+]?\d+(\.\d|\.\d[0-9])?$/  

but it just accepts up to 2 decimal places. What changes do I have to make, so that it can accept any number of decimal places?

Also where can I find a good tutorial for learning regex.

Thanks beforehand

like image 312
eddy Avatar asked Dec 16 '10 22:12

eddy


4 Answers

This would be my way: ^[+]?\d+([.]\d+)?$
EDIT: If you want to allow something like .23, you could use ^[+]?([.]\d+|\d+([.]\d+)?)$
EDIT: tchrist insists on this one: allowing 4., you could use ^[+]?([.]\d+|\d+[.]?\d*)$

Explanation:

  • with or without positive sign
  • one or more digits
  • with or without:
    • decimal point
    • one or more digits

Note: This will not accept a negative number, which is what you ultimately want.

like image 147
BeemerGuy Avatar answered Oct 16 '22 06:10

BeemerGuy


The short answer is that you need this pattern:

^(?!(?:^[-+]?[0.]+(?:[Ee]|$)))(?!(?:^-))(?:(?:[+-]?)(?=[0123456789.])(?:(?:(?:[0123456789]+)(?:(?:[.])(?:[0123456789]*))?|(?:(?:[.])(?:[0123456789]+))))(?:(?:[Ee])(?:(?:[+-]?)(?:[0123456789]+))|))$

The long answer is contained in the following program:

#!/usr/bin/perl

use strict;
use warnings qw<FATAL all>;

my $number_rx = qr{

  # leading sign, positive or negative
    (?: [+-] ? )

  # mantissa
    (?= [0123456789.] )
    (?:
        # "N" or "N." or "N.N"
        (?:
            (?: [0123456789] +     )
            (?:
                (?: [.] )
                (?: [0123456789] * )
            ) ?
      |
        # ".N", no leading digits
            (?:
                (?: [.] )
                (?: [0123456789] + )
            )
        )
    )

  # abscissa
    (?:
        (?: [Ee] )
        (?:
            (?: [+-] ? )
            (?: [0123456789] + )
        )
        |
    )
}x;

my $negative_rx = qr{ ^ - }x;
my $zero_rx     = qr{ ^ [-+]? [0.]+ (?: [Ee] | $ ) }x;

my $positive_rx = qr{
    ^
    (?!  $zero_rx      )
    (?!  $negative_rx  )
    $number_rx
    $
}x;

my @test_data = qw{
    -2 2 +2 2. -1 1 +1 1.
    0 +0 -0 .0 0.
    1.3 -3.2 5.13.7
    00.00 +00 -00 +0-1
    0000.
    McGillicuddy
    +365.2425
    6.02e23
    .0000000000000000000000000000000000000000000000000000000000000000000
    .00000000000000000000000000000000000000000000000000000000000000000000000001
    .03 0.3 3.0
    0e50 0e-50
    1e50 1e+50 1e-50
};


for my $n (@test_data) {
    printf "%s is%s a positive number.\n",
            $n, $n =~ /$positive_rx/ ? "" : " not";
}

The test results are:

-2 is not a positive number.
2 is a positive number.
+2 is a positive number.
2. is a positive number.
-1 is not a positive number.
1 is a positive number.
+1 is a positive number.
1. is a positive number.
0 is not a positive number.
+0 is not a positive number.
-0 is not a positive number.
.0 is not a positive number.
0. is not a positive number.
1.3 is a positive number.
-3.2 is not a positive number.
5.13.7 is not a positive number.
00.00 is not a positive number.
+00 is not a positive number.
-00 is not a positive number.
+0-1 is not a positive number.
0000. is not a positive number.
McGillicuddy is not a positive number.
+365.2425 is a positive number.
6.02e23 is a positive number.
.0000000000000000000000000000000000000000000000000000000000000000000 is not a positive number.
.00000000000000000000000000000000000000000000000000000000000000000000000001 is a positive number.
.03 is a positive number.
0.3 is a positive number.
3.0 is a positive number.
0e50 is not a positive number.
0e-50 is not a positive number.
1e50 is a positive number.
1e+50 is a positive number.
1e-50 is a positive number.
like image 44
tchrist Avatar answered Oct 16 '22 05:10

tchrist


This should do it.

\+?(\d+(\.(\d+)?)?|\.\d+)

There are tons of regular expression tutorials out there, here is one of them:

http://www.cs.tut.fi/~jkorpela/perl/regexp.html

like image 33
killdash9 Avatar answered Oct 16 '22 06:10

killdash9


This one is kinda simple — /\d*(\.d*)?/g

Update: this one doesn't match empty strings — /(\.)?\d+(\.\d*)?/g
Tested on "-1.5 0 12. -123.4. 15 -2. .4"

like image 36
Alex Shevchenko Avatar answered Oct 16 '22 04:10

Alex Shevchenko