How do I include negative decimal numbers in this regular expression?

How can I match negative numbers with this regular expression? This regular expression works fine with positive values, but I want it to also allow negative values, for example. -10, -125.5 etc.

^[0-9]\d*(\.\d+)?$ 

thank

+61
regex numbers
Apr 04
source share
13 answers

You must add an optional hyphen at the beginning by adding -? ( ? - quantifier , which means one or zero occurrence):

 ^-?[0-9]\d*(\.\d+)?$ 

I tested it in Rubular with these values:

 10.00 -10.00 

and both are as expected.

+123
Apr 04 '13 at 14:52
source share

Some examples of regular expressions:

Positive integers :

 ^\d+$ 

Negative integers :

 ^-\d+$ 

Integer

 ^-?\d+$ 

Positive number :

 ^\d*\.?\d+$ 

Negative number :

 ^-\d*\.?\d+$ 

Positive number or negative number :

 ^-?\d*\.{0,1}\d+$ 

Phone number :

 ^\+?[\d\s]{3,}$ 

Phone with code :

 ^\+?[\d\s]+\(?[\d\s]{10,}$ 

Year 1900-2099 :

 ^(19|20)[\d]{2,2}$ 

Date (dd mm yyyy, d / m / yyyy, etc.):

 ^([1-9]|0[1-9]|[12][0-9]|3[01])\D([1-9]|0[1-9]|1[012])\D(19[0-9][0-9]|20[0-9][0-9])$ 

IP v4 :

 ^(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]){3}$ 
+95
Apr 04 '13 at 14:53 on
source share

I do not know why you need this first [0-9] .

Try:

 ^-?\d*(.\d+)?$ 

Update

If you want to be sure that you will have a digit in one place, use

 ^-?\d+(.\d+)?$ 
+9
Apr 04 '13 at 14:53 on
source share

This will allow the use of the - or + symbol only after the following number:

  ^([+-](?=\.?\d))?(\d+)?(\.\d+)?$ 
+1
Sep 15 '13 at 21:08
source share

UPDATED (08/13/2014): This is the best code for positive and negative numbers =)

 (^-?0\.[0-9]*[1-9]+[0-9]*$)|(^-?[1-9]+[0-9]*((\.[0-9]*[1-9]+[0-9]*$)|(\.[0-9]+)))|(^-?[1-9]+[0-9]*$)|(^0$){1} 

I tried with these numbers and worked great:

 -1234454.3435 -98.99 -12.9 -12.34 -10.001 -3 -0.001 -000 -0.00 0 0.00 00000001.1 0.01 1201.0000001 1234454.3435 7638.98701 
+1
Aug 13 '14 at 5:07
source share

This will allow both positive and negative integers.

ValidationExpression = "^ -? [0-9] \ d * (\ d +) $"

+1
Dec 08 '14 at 12:34
source share

I have some regex experiments in django url that require negative to positive numbers

 ^(?P<pid>(\-\d+|\d+))$ 

If we focus on this part (\-\d+|\d+) and ignore the others, this semicolon | means OR in the regular expression, then a negative value will correspond to this part \-\d+ , and a positive value will be \d+

+1
Mar 31 '15 at 4:14
source share

Just add token 0 or 1:

 ^-?[0-9]\d*(.\d+)?$ 
0
Apr 04
source share

Only for a negative number is this ideal.

 ^-\d*\.?\d+$ 
0
Aug 23 '17 at 17:24
source share

Regular expression for a number, optional decimal point, optional minus:

 ^-?(\d*\.)?\d+$; 

works for negative integer, decimal, negative with decimal

0
Feb 09 '18 at 6:29
source share

This worked for me, allowing both negative and positive numbers :

  \-*\d+ 

When using C #:

 Regex.Match(someString, @"\-*\d+").Value; 
0
Mar 12 '18 at 20:37
source share
 ^(-?\d+\.)?-?\d+$ 

permit:

 23425.23425 10.10 100 0 0.00 -100 -10.10 10.-10 -10.-10 -23425.23425 -23425.-23425 0.234 
0
Jan 26 '19 at 10:09
source share
 ^[+-]?\d{1,18}(\.\d{1,2})?$ 

accepts positive or negative decimal values.

-one
Dec 23 '17 at 10:30
source share



All Articles