如何在这个正则表达式中包含负十进制数?

如何用这个正则表达式来匹配负数?这个正则表达式可以很好地处理正值,但是我希望它也允许负值,例如 -10,-125.5等等。

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

谢谢

243587 次浏览

你应该在开头添加一个可选的连字符 -?(? 量词 ,意思是 一次或零次) :

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

我在 Rubular 用这些价值观验证了它:

10.00
-10.00

两者都符合预期。

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'));

一些正则表达式示例:

正整数 :

^\d+$

负整数 :

^-\d+$

Integer:

^-?\d+$

正数 :

^\d*\.?\d+$

负数 :

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

正数或负数 :

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

电话号码 :

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

电话号码 :

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

一九零零至二零九九年

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

日期 (dd mm yyyyy,d/m/yyyy 等) :

^([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}$

我不知道你为什么需要第一个 [0-9]

试试:

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

更新

如果您想确保您将在一个位置上有一个数字,那么使用

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

只需添加一个0或1个标记:

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

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

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

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}

我试过这些数字,效果不错:

-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

这将同时允许正整数和负整数

ValidationExpression = “ ^-? [0-9] d * (d +) ? $”

我有一些关于 django url 中正则表达式的实验,它需要从负数到正数

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

让我们关注这个 (\-\d+|\d+)部分,忽略其他部分,这个分号 |在正则表达式中表示 或者,然后负值将与这个 \-\d+部分匹配,正值将进入这个 \d+

对于负数,这是完美的。

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

接受正的或负的小数值。

数字的正则表达式,可选小数点,可选负数:

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

工程负整数,十进制,负与十进制

这招对我很管用:

 \-*\d+

如果使用 C # :

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

allow:

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

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);

[0-9] expression前面加上 "-" minus? quantifier就可以了。

-?[0-9]

作为参考

  • ^b-^ quantifier匹配任何以“ b”开头的字符串
  • c?-? quantifier匹配0或1个出现的“ c”
  • [0-9]-查找 [] brackets之间的任何字符
  • 0-9中找到一个数字
  • d*-* quantifier匹配零个或多个出现的“ d”
  • "." character匹配
  • z+-+ quantifier匹配一个或多个出现的“ z”
  • e$-$ quantifier与“ e”结尾的任何字符串匹配

希望,这将有助于理解问题 中发布的正则表达式。

简单地说,/\d/在我能想到的所有情况下都能正常工作:

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);
}

想要了解更多的 Math乐趣,请查看 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math

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*))$

这保证了 应该工作 威尔工作的一切,包括:

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

该表达式还拒绝淘气的孩子可能输入的内容,这些内容虽然仍然是有效的字符输入,但不会是有效的数字,例如:

+.
-
.
(nul or newline)

我发现大多数表达式都写在这里(以 \d+$结尾) ,如果它们包含一个小数点后面没有任何数字,那么它们将拒绝数字。如果将该表达式改为以 \d*结尾,那么整个表达式将是可选的,从而使其与上面第二个列表中的条目相匹配。但是通过使用带有布尔 OR运算符(|)的捕获组来要求在 < strong > 或之前 小数点之后至少有一个数字,就覆盖了所有的基数。