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

RegexNumbers

Regex Problem Overview


How do I match negative numbers as well by this regular expression? This regex works fine with positive values, but I want it to also allow negative values e.g. -10, -125.5 etc.

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

Thanks

Regex Solutions


Solution 1 - Regex

You should add an optional hyphen at the beginning by adding -? (? is a quantifier meaning one or zero occurrences):

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

I verified it in Rubular with these values:

10.00
-10.00

and both matched as expected.

let r = new RegExp(/^-?[0-9]\d*(\.\d+)?$/);

//true
console.log(r.test('10'));
console.log(r.test('10.0'));
console.log(r.test('-10'));
console.log(r.test('-10.0'));
//false
console.log(r.test('--10'));
console.log(r.test('10-'));
console.log(r.test('1-0'));
console.log(r.test('10.-'));
console.log(r.test('10..0'));
console.log(r.test('10.0.1'));

Solution 2 - Regex

Some Regular expression examples:

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}$

Solution 3 - Regex

I don't know why you need that first [0-9].

Try:

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

Update

If you want to be sure that you'll have a digit on the ones place, then use

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

Solution 4 - Regex

UPDATED(13/08/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 this numbers and works fine:

-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

Solution 5 - Regex

This will allow a - or + character only when followed by a number:

 ^([+-](?=\.?\d))?(\d+)?(\.\d+)?$

Solution 6 - Regex

I have some experiments about regex in django url, which required from negative to positive numbers

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

Let's we focused on this (\-\d+|\d+) part and ignoring others, this semicolon | means OR in regex, then the negative value will match with this \-\d+ part, and positive value into this \d+

Solution 7 - Regex

Adding "-" minus followed by ? quantifier in front of [0-9] expression should do the work.

-?[0-9]

For reference

  • ^b - ^ quantifier matches for any string begin with "b"
  • c? - ? quantifier matches for 0 or 1 occurrence of "c"
  • [0-9] - find any character between the [] brackets
  • \d - find a digit from 0-9
  • d* - * quantifier matches for zero or more occurrence of "d"
  • \. - matches a "." character
  • z+ - + quantifier matches for one or more occurrence of "z"
  • e$ - $ quantifier matches for any string end with "e"

Hope, it'll help to understand posted regex in the question.

Solution 8 - Regex

This will allow both positive and negative integers > ValidationExpression="^-?[0-9]\d*(\d+)?$"

Solution 9 - Regex

^[+-]?\d{1,18}(\.\d{1,2})?$

accepts positive or negative decimal values.

Solution 10 - Regex

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

 \-*\d+

If using C#:

Regex.Match(someString, @"\-*\d+").Value;

Solution 11 - Regex

If you have this val="-12XXX.0abc23" and you want to extract only the decimal number, in this case this regex (^-?[0-9]\d*(\.\d+)?$) will not help you to achieve it.
this is the proper code with the correct detection regex:

var val="-12XXX.0abc23";
val = val.replace(/^\.|[^-?\d\.]|\.(?=.*\.)|^0+(?=\d)/g, '');
console.log(val);

Solution 12 - Regex

Just add a 0 or 1 token:

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

Solution 13 - Regex

For negative number only, this is perfect.

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

Solution 14 - Regex

Regular expression for number, optional decimal point, optional negative:

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

works for negative integer, decimal, negative with decimal

Solution 15 - Regex

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

allow:

23425.23425
10.10
100
0
0.00
-100
-10.10
10.-10
-10.-10
-23425.23425
-23425.-23425
0.234

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1786107View Question on Stackoverflow
Solution 1 - RegexMike PerrenoudView Answer on Stackoverflow
Solution 2 - RegexKF2View Answer on Stackoverflow
Solution 3 - RegexAndre CalilView Answer on Stackoverflow
Solution 4 - RegexPacho ZuñigaView Answer on Stackoverflow
Solution 5 - RegexIan TimothyView Answer on Stackoverflow
Solution 6 - RegexAditya Kresna PermanaView Answer on Stackoverflow
Solution 7 - RegexRoslin Mahmud JoyView Answer on Stackoverflow
Solution 8 - RegexPrajwal RaiView Answer on Stackoverflow
Solution 9 - RegexDeepak TambeView Answer on Stackoverflow
Solution 10 - RegexusefulBeeView Answer on Stackoverflow
Solution 11 - RegexBasilView Answer on Stackoverflow
Solution 12 - RegexBanksySanView Answer on Stackoverflow
Solution 13 - RegexKamleshkumar GujarathiView Answer on Stackoverflow
Solution 14 - RegexviveshView Answer on Stackoverflow
Solution 15 - RegexalchemistView Answer on Stackoverflow