131

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

Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103
user1786107
  • 2,921
  • 5
  • 24
  • 35

17 Answers17

247

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'));
OXiGEN
  • 2,041
  • 25
  • 19
Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
  • 4
    you should escape the dot, i.e. `^-?[0-9]\d*(\.\d+)?$` – Alex Shesterov Apr 04 '13 at 14:54
  • 21
    If there is a potential to need to capture explicitly possitive numbers (+10.00, used in some applications) you could use `^[-+]?[0-9]\d*(\.\d+)?$` – Cemafor Apr 04 '13 at 14:55
  • 4
    why not replacing `[0-9]\d*` with `\d+`? so `^[-+]?\d+(\.\d+)?$`? – Amin_mmz Mar 16 '21 at 10:23
  • 1
    And do you even need the group? `^[-+]?\d*.?\d*$` matches `3`, `3.`, `0.3`, and `.3`. Might be more useful depending on the application (e.g. input validation). – nullromo Sep 23 '22 at 18:09
164

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}$
Regexident
  • 29,441
  • 10
  • 93
  • 100
KF2
  • 9,887
  • 8
  • 44
  • 77
  • The first example for positive integers `^\d+$` includes zero, but 0 is neither positive nor negative. For positive non-zero integers, try `^[1-9]\d*$` – Joel Jan 03 '23 at 09:14
14

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+)?$
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Andre Calil
  • 7,652
  • 34
  • 41
  • 1
    Probably just to guarantee a digit in the ones place, `^-?\d+(\.\d+)?$` should work if that's the case – Cemafor Apr 04 '13 at 15:01
7

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.

3

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
2

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

 ^([+-](?=\.?\d))?(\d+)?(\.\d+)?$
Ian Timothy
  • 2,623
  • 3
  • 15
  • 10
2

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+

Aditya Kresna Permana
  • 11,869
  • 8
  • 42
  • 48
2

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);
Basil
  • 1,613
  • 12
  • 25
2

I had a case recently where students were entering only the accepted characters in a numeric response field, yet still managed to break things. Thus, I ended up using the following catch-all.

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

This ensures everything that should work will work, including:

0
.0
0.0
-.11
+.2
-0.2
+01.
-123.
+123.4567890
-012.0
+1
-1.

The expression also rejects things that mischievous kids might enter which, while still being valid character input, would not be a valid number, such as:

+.
-
.
(nul or newline)

I found that the expression as most have it written here (ending with \d+$) will reject numbers if they include a decimal point without any numbers after it. And making that expression instead end with \d* would make the entire expression optional, thus causing it to match the entries in the second list above. But by using the capturing group with the boolean OR operator (|) to require at least one digit either after or before a decimal point, all bases are covered.

Max Well
  • 257
  • 2
  • 9
1

This will allow both positive and negative integers

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

Jaykumar Patel
  • 26,836
  • 12
  • 74
  • 76
1
^[+-]?\d{1,18}(\.\d{1,2})?$

accepts positive or negative decimal values.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
1

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

 \-*\d+

If using C#:

Regex.Match(someString, @"\-*\d+").Value;
usefulBee
  • 9,250
  • 10
  • 51
  • 89
0

Just add a 0 or 1 token:

^-?[0-9]\d*(.\d+)?$
BanksySan
  • 27,362
  • 33
  • 117
  • 216
0

For negative number only, this is perfect.

^-\d*\.?\d+$
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
0

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

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

works for negative integer, decimal, negative with decimal

Hexfire
  • 5,945
  • 8
  • 32
  • 42
vivesh
  • 1
0
^(-?\d+\.)?-?\d+$

allow:

23425.23425
10.10
100
0
0.00
-100
-10.10
10.-10
-10.-10
-23425.23425
-23425.-23425
0.234
alchemist
  • 37
  • 3
0

Simply /\d/ works as expected for all cases I can think of:

let ns = {
  regex: {
    num: /\d/
  }
}

for (let i of [-2, -1, 0, 1, 2, 'one', 'negative one', Math.PI, Math.E, (42 * -1), (42 / -1), '-1', '0', '1', 1.01, -1.01, .1, -.1, Math.sqrt(42)]) {
  console.log(ns.regex.num.test(i) + ': ' + i);
}

For more Math fun, check out https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math

OXiGEN
  • 2,041
  • 25
  • 19