Reputation: 3071
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
Upvotes: 138
Views: 306534
Reputation: 1
Here is my simple variant of regex exspression for finding both negative and positive int and float numbers in code:
(\b|\B-)\d*(\.\d+)?
Works fine for any number variable declarations:
float x = -0.25;
int y = 123;
etc...
Upvotes: 0
Reputation: 337
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.
Upvotes: 4
Reputation: 2399
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
Upvotes: 0
Reputation: 180
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.
Upvotes: 11
Reputation: 67898
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'));
Upvotes: 263
Reputation: 7692
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+)?$
Upvotes: 15
Reputation: 1651
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);
Upvotes: 3
Reputation: 55
^(-?\d+\.)?-?\d+$
allow:
23425.23425
10.10
100
0
0.00
-100
-10.10
10.-10
-10.-10
-23425.23425
-23425.-23425
0.234
Upvotes: 0
Reputation: 9692
This worked for me, allowing both negative and positive numbers:
\-*\d+
If using C#:
Regex.Match(someString, @"\-*\d+").Value;
Upvotes: 3
Reputation: 1
Regular expression for number, optional decimal point, optional negative:
^-?(\d*\.)?\d+$;
works for negative integer, decimal, negative with decimal
Upvotes: 0
Reputation: 49
^[+-]?\d{1,18}(\.\d{1,2})?$
accepts positive or negative decimal values.
Upvotes: 1
Reputation: 155
For negative number only, this is perfect.
^-\d*\.?\d+$
Upvotes: 0
Reputation: 12099
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+
Upvotes: 3
Reputation: 21
This will allow both positive and negative integers
ValidationExpression="^-?[0-9]\d*(\d+)?$"
Upvotes: 1
Reputation: 51
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
Upvotes: 4
Reputation: 2953
This will allow a -
or +
character only when followed by a number:
^([+-](?=\.?\d))?(\d+)?(\.\d+)?$
Upvotes: 3
Reputation: 10143
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}$
Upvotes: 170