Javascript 用于查找十进制/浮点数的正则表达式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10256061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:30:45  来源:igfitidea点击:

regular expression for finding decimal/float numbers?

javascriptregexfloating-pointdecimal

提问by MBehtemam

i need a regular expression for decimal/float numbers like 12 12.2 1236.32 123.333 and +12.00 or -12.00 or ...123.123... for using in javascript and jQuery. Thank you.

我需要一个十进制/浮点数的正则表达式,如 12 12.2 1236.32 123.333 和 +12.00 或 -12.00 或 ...123.123... 用于在 javascript 和 jQuery 中使用。谢谢你。

回答by Paul

Optionally match a + or - at the beginning, followed by one or more decimal digits, optional followed by a decimal point and one or more decimal digits util the end of the string:

可选地在开头匹配一个 + 或 -,后跟一个或多个十进制数字,可选后跟一个小数点和一个或多个十进制数字 util 字符串的结尾:

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

RegexPal

正则表达式

回答by lbsweek

The right expression should be as followed:

正确的表达方式应该如下:

[+-]?([0-9]*[.])?[0-9]+

this apply for:

这适用于:

+1
+1.
+.1
+0.1
1
1.
.1
0.1

Here is Python example:

这是 Python 示例:

import re
#print if found
print(bool(re.search(r'[+-]?([0-9]*[.])?[0-9]+', '1.0')))
#print result
print(re.search(r'[+-]?([0-9]*[.])?[0-9]+', '1.0').group(0))

Output:

输出:

True
1.0

If you are using mac, you can test on command line:

如果你使用的是 mac,你可以在命令行上测试:

python -c "import re; print(bool(re.search(r'[+-]?([0-9]*[.])?[0-9]+', '1.0')))"

python -c "import re; print(re.search(r'[+-]?([0-9]*[.])?[0-9]+', '1.0').group(0))"

回答by Swathi

You can check for text validation and also only one decimal point validation using isNaN

您可以使用 isNaN 检查文本验证以及仅一位小数点验证

var val = $('#textbox').val();
var floatValues =  /[+-]?([0-9]*[.])?[0-9]+/;
 if (val.match(floatValues) && !isNaN(val)) {
  // your function
}

回答by Rod

This is an old post but it was the top search result for "regular expression for floating point" or something like that and doesn't quite answer _my_ question. Since I worked it out I will share my result so the next person who comes across this thread doesn't have to work it out for themselves.

这是一篇旧帖子,但它是“浮点正则表达式”或类似内容的顶级搜索结果,并没有完全回答_我的_问题。既然我已经解决了,我将分享我的结果,这样下一个遇到这个线程的人就不必自己解决了。

All of the answers thus far accept a leading 0on numbers with two (or more) digits on the left of the decimal point (e.g. 0123instead of just 123) This isn't really valid and in some contexts is used to indicate the number is in octal (base-8) rather than the regular decimal (base-10) format.

到目前为止,所有答案都接受0在小数点左侧有两个(或更多)数字的数字前导(例如,0123而不是仅仅123)这不是真正有效的,在某些情况下用于指示数字是八进制的(base-8) 而不是常规的十进制 (base-10) 格式。

Also these expressions accept a decimal with no leading zero (.14instead of 0.14) or without a trailing fractional part (3.instead of 3.0). That is valid in some programing contexts (including JavaScript) but I want to disallow them (because for my purposes those are more likely to be an error than intentional).

此外,这些表达式接受没有前导零(.14代替0.14)或没有尾随小数部分(3.代替3.0)的小数。这在某些编程环境(包括 JavaScript)中是有效的,但我想禁止它们(因为就我而言,这些更可能是错误而不是故意的)。

Ignoring "scientific notation" like 1.234E7, here is an expression that meets my criteria:

忽略“科学记数法”之类的1.234E7,这里有一个符合我标准的表达式:

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

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

or if you really want to accept a leading +, then:

或者如果你真的想接受一个领先的+,那么:

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

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

I believe that regular expression will perform a strict test for the typical integer or decimal-style floating point number.

我相信正则表达式会对典型的整数或十进制式浮点数进行严格的测试。

When matched:

匹配时:

  • $1contains the full number that matched
  • $2contains the (possibly empty) leading sign (+/-)
  • $3contains the value to the left of the decimal point
  • $5contains the value to the right of the decimal point, including the leading .
  • $1包含匹配的完整数字
  • $2包含(可能为空)前导符号 (+/-)
  • $3包含小数点左边的值
  • $5包含小数点右边的值,包括前导 .

By "strict" I mean that the number must be the only thing in the string you are testing.

“严格”是指数字必须是您正在测试的字符串中的唯一内容。

If you want to extract just the float value out of a string that contains other content use this expression:

如果您只想从包含其他内容的字符串中提取浮点值,请使用以下表达式:

/((\b|\+|-)(0|([1-9][0-9]*))(\.[0-9]+)?)\b/

/((\b|\+|-)(0|([1-9][0-9]*))(\.[0-9]+)?)\b/

Which will find -3.14in "negative pi is approximately -3.14."or in "(-3.14)"etc.

哪个会找到-3.14in"negative pi is approximately -3.14."或 in"(-3.14)"等。

The numbered groups have the same meaning as above (except that $2is now an empty string ("") when there is no leading sign, rather than null).

编号组具有与上述相同的含义(除了当没有前导符号时$2现在是空字符串 ( "") 而不是null)。

But be aware that it will also try to extract whatever numbers it can find. E.g., it will extract 127.0from 127.0.0.1.

但请注意,它还会尝试提取它可以找到的任何数字。例如,它将127.0127.0.0.1.

If you want something more sophisticated than that then I think you might want to look at lexical analysis instead of regular expressions. I'm guessing one could create a look-ahead-based expression that would recognize that "Pi is 3.14."contains a floating point number but Home is 127.0.0.1.does not, but it would be complex at best. If your pattern depends on the characters that come after it in non-trivial ways you're starting to venture outside of regular expressions' sweet-spot.

如果您想要比这更复杂的东西,那么我认为您可能需要查看词法分析而不是正则表达式。我猜可以创建一个基于前瞻的表达式,它可以识别"Pi is 3.14."包含浮点数但Home is 127.0.0.1.不包含浮点数 的表达式,但充其量它会很复杂。如果您的模式以非平凡的方式依赖于它后面的字符,那么您就开始在正则表达式的最佳位置之外冒险。

回答by duparq

Paulpro and lbsweek answers led me to this:

Paulpro 和 lbsweek 的回答让我想到了这个:

re=/^[+-]?(?:\d*\.)?\d+$/;
>> /^[+-]?(?:\d*\.)?\d+$/

re.exec("1")
>> Array [ "1" ]

re.exec("1.5")
>> Array [ "1.5" ]

re.exec("-1")
>> Array [ "-1" ]

re.exec("-1.5")
>> Array [ "-1.5" ]

re.exec(".5")
>> Array [ ".5" ]

re.exec("")
>> null

re.exec("qsdq")
>> null