I am trying to use a regular expression validation to check for only decimal values or numeric values. But user enters numeric value, it don't be first digit "0"
我正在尝试使用正则表达式验证只检查十进制值或数值。但用户输入数值,不是第一个数字"0"
How do I do that?
我该怎么做呢?
10 个解决方案
#1
102
A digit in the range 1-9 followed by zero or more other digits:
范围为1-9的数字,其后是零位或以上的数字:
^[1-9]\d*$
To allow numbers with an optional decimal point followed by digits. A digit in the range 1-9 followed by zero or more other digits then optionally followed by a decimal point followed by at least 1 digit:
允许使用可选小数点后的数字。范围为1-9的一位数,后面跟着零或更多的其他位数,然后可选地跟着小数点,后面跟着至少一位数:
^[1-9]\d*(\.\d+)?$
Notes:
注:
-
The
^
and$
anchor to the start and end basically saying that the whole string must match the pattern开始和结束的^和$锚基本上说整个字符串必须匹配模式
-
()?
matches 0 or 1 of the whole thing between the brackets()?在括号之间匹配0或1。
Update to handle commas:
更新处理逗号:
In regular expressions .
has a special meaning - match any single character. To match literally a .
in a string you need to escape the .
using \.
This is the meaning of the \.
in the regexp above. So if you want to use comma instead the pattern is simply:
在正则表达式。有一个特殊的意义-匹配任何一个字符。匹配字面上的a。在一个字符串中,你需要逃离。使用\。这就是\的意思。在上面的regexp。所以,如果你想用逗号代替逗号,模式很简单:
^[1-9]\d*(,\d+)?$
Further update to handle commas and full stops
进一步更新以处理逗号和句号
If you want to allow a . between groups of digits and a , between the integral and the fractional parts then try:
如果你想允许a。在数和a组之间,积分和小数部分之间,然后尝试:
^[1-9]\d{0,2}(\.\d{3})*(,\d+)?$
i.e. this is a digit in the range 1-9 followed by up to 2 other digits then zero or more groups of a full stop followed by 3 digits then optionally your comma and digits as before.
这是在1-9范围内的一个数字,然后是另外2个数字,然后是0或更多的一组,然后是3位数字,然后是你的逗号和数字。
If you want to allow a . anywhere between the digits then try:
如果你想允许a。在数字之间的任何地方,然后尝试:
^[1-9][\.\d]*(,\d+)?$
i.e. a digit 1-9 followed by zero or more digits or full stops optionally followed by a comma and one or more digits.
也就是说,数字1-9后面跟着零或多个数字,或者可以选择的句号后面跟着一个逗号和一个或多个数字。
#2
45
Actually, none of the given answers are fully cover the request.
As the OP didn't provided a specific use case or types of numbers, I will try to cover all possible cases and permutations.
实际上,所有给出的答案都不能完全满足要求。由于OP没有提供特定的用例或数字类型,我将尝试涵盖所有可能的情况和排列。
Regular Numbers
Whole Positive
This number is usually called unsigned integer, but you can also call it a positive non-fractional number, include zero. This includes numbers like 0
, 1
and 99999
.
The Regular Expression that covers this validation is:
这个数字通常被称为无符号整数,但是你也可以称它为正的非小数,包括零。这包括像0,1,99999这样的数字。涵盖此验证的正则表达式是:
/^(0|[1-9]\d*)$/
测试这个正则表达式
Whole Positive and Negative
This number is usually called signed integer, but you can also call it a non-fractional number. This includes numbers like 0
, 1
, 99999
, -99999
, -1
and -0
.
The Regular Expression that covers this validation is:
这个数字通常被称为带符号整数,但也可以称之为非小数。这包括像0,1,99999,-99999,-1和-0这样的数字。涵盖此验证的正则表达式是:
/^-?(0|[1-9]\d*)$/
测试这个正则表达式
As you probably noticed, I have also included -0
as a valid number. But, some may argue with this usage, and tell that this is not a real number (you can read more about Signed Zero here). So, if you want to exclude this number from this regex, here's what you should use instead:
正如您可能注意到的,我还将-0作为一个有效数字。但是,有些人可能会反对这种用法,并认为这不是一个真正的数字(您可以在这里阅读更多关于符号0的内容)。所以,如果你想从这个regex中排除这个数字,你应该使用以下方法:
/^-?(0|[1-9]\d*)(?<!-0)$/
测试这个正则表达式
All I have added is (?<!-0)
, which means not to include -0
before this assertion. This (?<!...)
assertion called negative lookbehind, which means that any phrase replaces the ...
should not appear before this assertion. Lookbehind has limitations, like the phrase cannot include quantifiers. That's why for some cases I'll be using Lookahead instead, which is the same, but in the opposite way.
我所添加的是(?
Many regex flavors, including those used by Perl and Python, only allow fixed-length strings. You can use literal text, character escapes, Unicode escapes other than
\X
, and character classes. You cannot use quantifiers or backreferences. You can use alternation, but only if all alternatives have the same length. These flavors evaluate lookbehind by first stepping back through the subject string for as many characters as the lookbehind needs, and then attempting the regex inside the lookbehind from left to right.许多regex风格,包括Perl和Python使用的那些,只允许固定长度的字符串。您可以使用文字文本、字符转义、Unicode转义除\X和字符类。不能使用量词或反向引用。你可以使用交替,但前提是所有的选项都有相同的长度。这些风格通过首先在主题字符串中按lookbehind所需的字符数量返回来评估lookbehind,然后尝试从左到右在lookbehind中使用regex。
You can read more bout Lookaround assertions here.
您可以在这里阅读更多的about Lookaround断言。
Fractional Numbers
Positive
This number is usually called unsigned float or unsigned double, but you can also call it a positive fractional number, include zero. This includes numbers like 0
, 1
, 0.0
, 0.1
, 1.0
, 99999.000001
, 5.10
.
The Regular Expression that covers this validation is:
这个数字通常被称为无符号浮点数或无符号双,但你也可以称它为正分数,包括零。这包括0、1、0.0、0.1、1.0、99999.000001、5.10等数字。涵盖此验证的正则表达式是:
/^(0|[1-9]\d*)(\.\d+)?$/
测试这个正则表达式
Some may say, that numbers like .1
, .0
and .00651
(same as 0.1
, 0.0
and 0.00651
respectively) are also valid fractional numbers, and I cannot disagree with them. So here is a regex that is additionally supports this format:
有些人可能会说,像。1,。0和。00651(分别等于0.1,0.0和0.00651)这样的数字也是有效的小数,我不同意他们的观点。这是一个regex,它还支持这种格式:
/^(0|[1-9]\d*)?(\.\d+)?(?<=\d)$/
测试这个正则表达式
Negative and Positive
This number is usually called signed float or signed double, but you can also call it a fractional number. This includes numbers like 0
, 1
, 0.0
, 0.1
, 1.0
, 99999.000001
, 5.10
, -0
, -1
, -0.0
, -0.1
, -99999.000001
, 5.10
.
The Regular Expression that covers this validation is:
这个数字通常称为有符号浮点数或有符号双精度浮点数,但也可以称为小数。这包括0、1、0.0、0.1、1.0、99999.000001、5.10、-0、-1、-0.0、-0.1、-0.1、-1、- 0.01、5.10。涵盖此验证的正则表达式是:
/^-?(0|[1-9]\d*)(\.\d+)?$/
测试这个正则表达式
For non -0
believers:
为非0信徒:
/^(?!-0(\.0+)?$)-?(0|[1-9]\d*)(\.\d+)?$/
测试这个正则表达式
For those who want to support also the invisible zero representations, like .1
, -.1
, use the following regex:
对于那些也想支持无形零表示的人,如。1,-。1、使用以下regex:
/^-?(0|[1-9]\d*)?(\.\d+)?(?<=\d)$/
测试这个正则表达式
The combination of non -0
believers and invisible zero believers, use this regex:
非0信仰者和不可见的0信仰者的组合,使用这个规则:
/^(?!-0?(\.0+)?$)-?(0|[1-9]\d*)?(\.\d+)?(?<=\d)$/
测试这个正则表达式
Numbers with a Scientific Notation (AKA Exponential Notation)
Some may want to support in their validations, numbers with a scientific character e
, which is by the way, an absolutely valid number, it is created for shortly represent a very long numbers. You can read more about Scientific Notation here. These numbers are usually looks like 1e3
(which is 1000
), 1e-3
(which is 0.001) and are fully supported by many major programming languages (e.g. JavaScript). You can test it by checking if the expression '1e3'==1000
returns true
.
I will divide the support for all the above sections, including numbers with scientific notation.
有些人可能想要支持他们的验证,数字有一个科学的字符e,顺便说一下,这是一个绝对有效的数字,它是为不久的代表一个非常长的数字而创建的。你可以在这里阅读更多的科学符号。这些数字通常看起来像1e3(即1000),1e-3(即0.001),并且得到了许多主要编程语言(例如JavaScript)的完全支持。您可以通过检查表达式'1e3'= 1000是否返回true来测试它。我将对上面所有部分的支持进行划分,包括使用科学符号的数字。
Regular Numbers
Whole positive number regex validation, supports numbers like 6e4
, 16e-10
, 0e0
but also regular numbers like 0
, 11
:
全正数regex验证,支持6e4、16e-10、0e0等数字,也支持常规数字,如0,11:
/^(0|[1-9]\d*)(e-?(0|[1-9]\d*))?$/i
测试这个正则表达式
Whole positive and negative number regex validation, supports numbers like -6e4
, -16e-10
, -0e0
but also regular numbers like -0
, -11
and all the whole positive numbers above:
全正数和负数regex验证,支持-6e4、-16e-10、-0e0等数字,也支持-0、-11等普通数字以及以上所有正数:
/^-?(0|[1-9]\d*)(e-?(0|[1-9]\d*))?$/i
测试这个正则表达式
Whole positive and negative number regex validation for non -0
believers, same as the above, except now it forbids numbers like -0
, -0e0
, -0e5
and -0e-6
:
对于非-0信仰者的正负数字regex验证,与上面一样,除了现在它禁止像-0、-0e0、-0e5和-0e-6这样的数字:
/^(?!-0)-?(0|[1-9]\d*)(e-?(0|[1-9]\d*))?$/i
测试这个正则表达式
Fractional Numbers
Positive number regex validation, supports also the whole numbers above, plus numbers like 0.1e3
, 56.0e-3
, 0.0e10
and 1.010e0
:
正号regex验证,也支持以上所有数字,加上0.1e3、56.0 -3、0.0e10、1.010e0等数字:
/^(0|[1-9]\d*)(\.\d+)?(e-?(0|[1-9]\d*))?$/i
测试这个正则表达式
Positive number with invisible zero support regex validation, supports also the above positive numbers, in addition numbers like .1e3
, .0e0
, .0e-5
and .1e-7
:
带隐形0的正数支持regex验证,也支持上述正数,另外还有。1e3,。0e0,。0e-5和。1e-7:
/^(0|[1-9]\d*)?(\.\d+)?(?<=\d)(e-?(0|[1-9]\d*))?$/i
测试这个正则表达式
Negative and positive number regex validation, supports the positive numbers above, but also numbers like -0e3
, -0.1e0
, -56.0e-3
and -0.0e10
:
负数和正数regex验证,支持上面的正数,也支持-0e3、- -0.1e0、-56.0 -0.0 -0.0 -0.0 -3、-0.0e10等数字:
/^-?(0|[1-9]\d*)(\.\d+)?(e-?(0|[1-9]\d*))?$/i
测试这个正则表达式
Negative and positive number regex validation fro non -0
believers, same as the above, except now it forbids numbers like -0
, -0.00000
, -0.0e0
, -0.00000e5
and -0e-6
:
对非0信众的负和正数正则表达式验证,与上述相同,但现在它禁止了-0、-0.00000、-0.0e0、-0.00000e5和-0e-6的数字:
/^(?!-0(\.0+)?(e|$))-?(0|[1-9]\d*)(\.\d+)?(e-?(0|[1-9]\d*))?$/i
测试这个正则表达式
Negative and positive number with invisible zero support regex validation, supports also the above positive and negative numbers, in addition numbers like -.1e3
, -.0e0
, -.0e-5
and -.1e-7
:
负数和正数,不可见的零支持regex验证,也支持上面的正数和负数,以及像-这样的数字。1 e3 -。0 e0 -。0 e-5 -.1e-7:
/^-?(0|[1-9]\d*)?(\.\d+)?(?<=\d)(e-?(0|[1-9]\d*))?$/i
测试这个正则表达式
Negative and positive number with the combination of non -0
believers and invisible zero believers, same as the above, but forbids numbers like -.0e0
, -.0000e15
and -.0e-19
:
负数和正数与非0信仰者和不可见的零信仰者结合,如上所述,但禁止数字如-。0 e0 -。0000年e15 -.0e-19:
/^(?!-0?(\.0+)?(e|$))-?(0|[1-9]\d*)?(\.\d+)?(?<=\d)(e-?(0|[1-9]\d*))?$/i
测试这个正则表达式
Numbers with Hexadecimal Representation
In many programming languages, string representation of hexadecimal number like 0x4F7A
may be easily cast to decimal number 20346
.
Thus, one may want to support it in his validation script.
The following Regular Expression supports only hexadecimal numbers representations:
在许多编程语言中,十六进制数字(如0x4F7A)的字符串表示形式可以很容易地转换为十进制数字20346。因此,您可能希望在验证脚本中支持它。下面的正则表达式仅支持十六进制数字表示:
/^0x[0-9a-f]+$/i
测试这个正则表达式
All Permutations
These final Regular Expressions, support the invisible zero numbers.
这些最后的正则表达式,支持不可见的零数。
Signed Zero Believers
/^(-?(0|[1-9]\d*)?(\.\d+)?(?<=\d)(e-?(0|[1-9]\d*))?|0x[0-9a-f]+)$/i
测试这个正则表达式
Non Signed Zero Believers
/^((?!-0?(\.0+)?(e|$))-?(0|[1-9]\d*)?(\.\d+)?(?<=\d)(e-?(0|[1-9]\d*))?|0x[0-9a-f]+)$/i
测试这个正则表达式
Hope I covered all number permutations that are supported in many programming languages.
Good luck!
希望我涵盖了许多编程语言支持的所有数字排列。好运!
Oh, forgot to mention, that those who want to validate a number includes a thousand separator, you should clean all the commas (,
) first, as there may be any type of separator out there, you can't actually cover them all.
But you can remove them first, before the number validation:
哦,忘了说,那些想要验证一个数字的人包括一千个分隔符,您应该首先清除所有的逗号(,),因为可能有任何类型的分隔符,您实际上不能全部覆盖它们。但你可以先删除它们,在数字验证之前:
//JavaScript
function clearSeparators(number)
{
return number.replace(/,/g,'');
}
#3
7
I had the same problem, but I also wanted ".25" to be a valid decimal number. Here is my solution using JavaScript:
我也有同样的问题,但我也想。25"是一个有效的小数。下面是我使用JavaScript的解决方案:
function isNumber(v) {
// [0-9]* Zero or more digits between 0 and 9 (This allows .25 to be considered valid.)
// ()? Matches 0 or 1 things in the parentheses. (Allows for an optional decimal point)
// Decimal point escaped with \.
// If a decimal point does exist, it must be followed by 1 or more digits [0-9]
// \d and [0-9] are equivalent
// ^ and $ anchor the endpoints so tthe whole string must match.
return v.trim().length > 0 && v.trim().match(/^[0-9]*(\.[0-9]+)?$/);
}
Where my trim() method is
我的trim()方法在哪里
String.prototype.trim = function() {
return this.replace(/(^\s*|\s*$)/g, "");
};
Matthew DesVoigne
马修DesVoigne
#4
5
I've tested all given regexes but unfortunately none of them pass those tests:
我已经测试了所有给定的regex,但不幸的是没有一个通过这些测试:
String []goodNums={"3","-3","0","0.0","1.0","0.1"};
String []badNums={"001","-00.2",".3","3.","a",""," ","-"," -1","--1","-.1","-0", "2..3", "2-", "2...3", "2.4.3", "5-6-7"};
Here is the best I wrote that pass all those tests:
下面是我写的通过所有这些测试的最好的:
"^(-?0[.]\\d+)$|^(-?[1-9]+\\d*([.]\\d+)?)$|^0$"
#5
2
Here is a great working regex for numbers. This accepts number with commas and decimals.
这是一个非常有用的数字正则表达式。它接受带有逗号和小数的数字。
/^-?(?:\d+|\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/
#6
1
Here is my regex for validating numbers:
下面是我的regex验证数字:
^(-?[1-9]+\\d*([.]\\d+)?)$|^(-?0[.]\\d*[1-9]+)$|^0$
Valid numbers:
有效数字:
String []validNumbers={"3","-3","0","0.0","1.0","0.1","0.0001","-555","94549870965"};
Invalid numbers:
无效的数字:
String []invalidNumbers={"a",""," ","-","001","-00.2","000.5",".3","3."," -1","--1","-.1","-0"};
#7
0
if you need to validate decimal with dots, commas, positives and negatives try this:
如果您需要使用点、逗号、正数和负数验证小数,请尝试以下方法:
Object testObject = "-1.5";
boolean isDecimal = Pattern.matches("^[\\+\\-]{0,1}[0-9]+[\\.\\,]{1}[0-9]+$", (CharSequence) testObject);
Good luck.
祝你好运。
#8
0
Below is the perfect one for mentioned requirement :
以下是最适合提出的要求:
^[0-9]{1,3}(,[0-9]{3})*(([\\.,]{1}[0-9]*)|())$
#9
0
/([0-9]+[.,]*)+/ matches any number with or withot coma or dots
/([0-9]+[.,]*)+/匹配任何有昏迷或没有圆点的数字
it can match
它可以匹配
122
122,354
122.88
112,262,123.7678
bug: it also matches 262.4377,3883 ( but it doesn't matter parctically)
bug:它还匹配262.4377和3883(但它没有多少区别)
#10
0
Try this code, hope it will help you
试试这段代码,希望它能对你有所帮助
String regex = "(\\d+)(\\.)?(\\d+)?"; for integer and decimal like 232 232.12
#1
102
A digit in the range 1-9 followed by zero or more other digits:
范围为1-9的数字,其后是零位或以上的数字:
^[1-9]\d*$
To allow numbers with an optional decimal point followed by digits. A digit in the range 1-9 followed by zero or more other digits then optionally followed by a decimal point followed by at least 1 digit:
允许使用可选小数点后的数字。范围为1-9的一位数,后面跟着零或更多的其他位数,然后可选地跟着小数点,后面跟着至少一位数:
^[1-9]\d*(\.\d+)?$
Notes:
注:
-
The
^
and$
anchor to the start and end basically saying that the whole string must match the pattern开始和结束的^和$锚基本上说整个字符串必须匹配模式
-
()?
matches 0 or 1 of the whole thing between the brackets()?在括号之间匹配0或1。
Update to handle commas:
更新处理逗号:
In regular expressions .
has a special meaning - match any single character. To match literally a .
in a string you need to escape the .
using \.
This is the meaning of the \.
in the regexp above. So if you want to use comma instead the pattern is simply:
在正则表达式。有一个特殊的意义-匹配任何一个字符。匹配字面上的a。在一个字符串中,你需要逃离。使用\。这就是\的意思。在上面的regexp。所以,如果你想用逗号代替逗号,模式很简单:
^[1-9]\d*(,\d+)?$
Further update to handle commas and full stops
进一步更新以处理逗号和句号
If you want to allow a . between groups of digits and a , between the integral and the fractional parts then try:
如果你想允许a。在数和a组之间,积分和小数部分之间,然后尝试:
^[1-9]\d{0,2}(\.\d{3})*(,\d+)?$
i.e. this is a digit in the range 1-9 followed by up to 2 other digits then zero or more groups of a full stop followed by 3 digits then optionally your comma and digits as before.
这是在1-9范围内的一个数字,然后是另外2个数字,然后是0或更多的一组,然后是3位数字,然后是你的逗号和数字。
If you want to allow a . anywhere between the digits then try:
如果你想允许a。在数字之间的任何地方,然后尝试:
^[1-9][\.\d]*(,\d+)?$
i.e. a digit 1-9 followed by zero or more digits or full stops optionally followed by a comma and one or more digits.
也就是说,数字1-9后面跟着零或多个数字,或者可以选择的句号后面跟着一个逗号和一个或多个数字。
#2
45
Actually, none of the given answers are fully cover the request.
As the OP didn't provided a specific use case or types of numbers, I will try to cover all possible cases and permutations.
实际上,所有给出的答案都不能完全满足要求。由于OP没有提供特定的用例或数字类型,我将尝试涵盖所有可能的情况和排列。
Regular Numbers
Whole Positive
This number is usually called unsigned integer, but you can also call it a positive non-fractional number, include zero. This includes numbers like 0
, 1
and 99999
.
The Regular Expression that covers this validation is:
这个数字通常被称为无符号整数,但是你也可以称它为正的非小数,包括零。这包括像0,1,99999这样的数字。涵盖此验证的正则表达式是:
/^(0|[1-9]\d*)$/
测试这个正则表达式
Whole Positive and Negative
This number is usually called signed integer, but you can also call it a non-fractional number. This includes numbers like 0
, 1
, 99999
, -99999
, -1
and -0
.
The Regular Expression that covers this validation is:
这个数字通常被称为带符号整数,但也可以称之为非小数。这包括像0,1,99999,-99999,-1和-0这样的数字。涵盖此验证的正则表达式是:
/^-?(0|[1-9]\d*)$/
测试这个正则表达式
As you probably noticed, I have also included -0
as a valid number. But, some may argue with this usage, and tell that this is not a real number (you can read more about Signed Zero here). So, if you want to exclude this number from this regex, here's what you should use instead:
正如您可能注意到的,我还将-0作为一个有效数字。但是,有些人可能会反对这种用法,并认为这不是一个真正的数字(您可以在这里阅读更多关于符号0的内容)。所以,如果你想从这个regex中排除这个数字,你应该使用以下方法:
/^-?(0|[1-9]\d*)(?<!-0)$/
测试这个正则表达式
All I have added is (?<!-0)
, which means not to include -0
before this assertion. This (?<!...)
assertion called negative lookbehind, which means that any phrase replaces the ...
should not appear before this assertion. Lookbehind has limitations, like the phrase cannot include quantifiers. That's why for some cases I'll be using Lookahead instead, which is the same, but in the opposite way.
我所添加的是(?
Many regex flavors, including those used by Perl and Python, only allow fixed-length strings. You can use literal text, character escapes, Unicode escapes other than
\X
, and character classes. You cannot use quantifiers or backreferences. You can use alternation, but only if all alternatives have the same length. These flavors evaluate lookbehind by first stepping back through the subject string for as many characters as the lookbehind needs, and then attempting the regex inside the lookbehind from left to right.许多regex风格,包括Perl和Python使用的那些,只允许固定长度的字符串。您可以使用文字文本、字符转义、Unicode转义除\X和字符类。不能使用量词或反向引用。你可以使用交替,但前提是所有的选项都有相同的长度。这些风格通过首先在主题字符串中按lookbehind所需的字符数量返回来评估lookbehind,然后尝试从左到右在lookbehind中使用regex。
You can read more bout Lookaround assertions here.
您可以在这里阅读更多的about Lookaround断言。
Fractional Numbers
Positive
This number is usually called unsigned float or unsigned double, but you can also call it a positive fractional number, include zero. This includes numbers like 0
, 1
, 0.0
, 0.1
, 1.0
, 99999.000001
, 5.10
.
The Regular Expression that covers this validation is:
这个数字通常被称为无符号浮点数或无符号双,但你也可以称它为正分数,包括零。这包括0、1、0.0、0.1、1.0、99999.000001、5.10等数字。涵盖此验证的正则表达式是:
/^(0|[1-9]\d*)(\.\d+)?$/
测试这个正则表达式
Some may say, that numbers like .1
, .0
and .00651
(same as 0.1
, 0.0
and 0.00651
respectively) are also valid fractional numbers, and I cannot disagree with them. So here is a regex that is additionally supports this format:
有些人可能会说,像。1,。0和。00651(分别等于0.1,0.0和0.00651)这样的数字也是有效的小数,我不同意他们的观点。这是一个regex,它还支持这种格式:
/^(0|[1-9]\d*)?(\.\d+)?(?<=\d)$/
测试这个正则表达式
Negative and Positive
This number is usually called signed float or signed double, but you can also call it a fractional number. This includes numbers like 0
, 1
, 0.0
, 0.1
, 1.0
, 99999.000001
, 5.10
, -0
, -1
, -0.0
, -0.1
, -99999.000001
, 5.10
.
The Regular Expression that covers this validation is:
这个数字通常称为有符号浮点数或有符号双精度浮点数,但也可以称为小数。这包括0、1、0.0、0.1、1.0、99999.000001、5.10、-0、-1、-0.0、-0.1、-0.1、-1、- 0.01、5.10。涵盖此验证的正则表达式是:
/^-?(0|[1-9]\d*)(\.\d+)?$/
测试这个正则表达式
For non -0
believers:
为非0信徒:
/^(?!-0(\.0+)?$)-?(0|[1-9]\d*)(\.\d+)?$/
测试这个正则表达式
For those who want to support also the invisible zero representations, like .1
, -.1
, use the following regex:
对于那些也想支持无形零表示的人,如。1,-。1、使用以下regex:
/^-?(0|[1-9]\d*)?(\.\d+)?(?<=\d)$/
测试这个正则表达式
The combination of non -0
believers and invisible zero believers, use this regex:
非0信仰者和不可见的0信仰者的组合,使用这个规则:
/^(?!-0?(\.0+)?$)-?(0|[1-9]\d*)?(\.\d+)?(?<=\d)$/
测试这个正则表达式
Numbers with a Scientific Notation (AKA Exponential Notation)
Some may want to support in their validations, numbers with a scientific character e
, which is by the way, an absolutely valid number, it is created for shortly represent a very long numbers. You can read more about Scientific Notation here. These numbers are usually looks like 1e3
(which is 1000
), 1e-3
(which is 0.001) and are fully supported by many major programming languages (e.g. JavaScript). You can test it by checking if the expression '1e3'==1000
returns true
.
I will divide the support for all the above sections, including numbers with scientific notation.
有些人可能想要支持他们的验证,数字有一个科学的字符e,顺便说一下,这是一个绝对有效的数字,它是为不久的代表一个非常长的数字而创建的。你可以在这里阅读更多的科学符号。这些数字通常看起来像1e3(即1000),1e-3(即0.001),并且得到了许多主要编程语言(例如JavaScript)的完全支持。您可以通过检查表达式'1e3'= 1000是否返回true来测试它。我将对上面所有部分的支持进行划分,包括使用科学符号的数字。
Regular Numbers
Whole positive number regex validation, supports numbers like 6e4
, 16e-10
, 0e0
but also regular numbers like 0
, 11
:
全正数regex验证,支持6e4、16e-10、0e0等数字,也支持常规数字,如0,11:
/^(0|[1-9]\d*)(e-?(0|[1-9]\d*))?$/i
测试这个正则表达式
Whole positive and negative number regex validation, supports numbers like -6e4
, -16e-10
, -0e0
but also regular numbers like -0
, -11
and all the whole positive numbers above:
全正数和负数regex验证,支持-6e4、-16e-10、-0e0等数字,也支持-0、-11等普通数字以及以上所有正数:
/^-?(0|[1-9]\d*)(e-?(0|[1-9]\d*))?$/i
测试这个正则表达式
Whole positive and negative number regex validation for non -0
believers, same as the above, except now it forbids numbers like -0
, -0e0
, -0e5
and -0e-6
:
对于非-0信仰者的正负数字regex验证,与上面一样,除了现在它禁止像-0、-0e0、-0e5和-0e-6这样的数字:
/^(?!-0)-?(0|[1-9]\d*)(e-?(0|[1-9]\d*))?$/i
测试这个正则表达式
Fractional Numbers
Positive number regex validation, supports also the whole numbers above, plus numbers like 0.1e3
, 56.0e-3
, 0.0e10
and 1.010e0
:
正号regex验证,也支持以上所有数字,加上0.1e3、56.0 -3、0.0e10、1.010e0等数字:
/^(0|[1-9]\d*)(\.\d+)?(e-?(0|[1-9]\d*))?$/i
测试这个正则表达式
Positive number with invisible zero support regex validation, supports also the above positive numbers, in addition numbers like .1e3
, .0e0
, .0e-5
and .1e-7
:
带隐形0的正数支持regex验证,也支持上述正数,另外还有。1e3,。0e0,。0e-5和。1e-7:
/^(0|[1-9]\d*)?(\.\d+)?(?<=\d)(e-?(0|[1-9]\d*))?$/i
测试这个正则表达式
Negative and positive number regex validation, supports the positive numbers above, but also numbers like -0e3
, -0.1e0
, -56.0e-3
and -0.0e10
:
负数和正数regex验证,支持上面的正数,也支持-0e3、- -0.1e0、-56.0 -0.0 -0.0 -0.0 -3、-0.0e10等数字:
/^-?(0|[1-9]\d*)(\.\d+)?(e-?(0|[1-9]\d*))?$/i
测试这个正则表达式
Negative and positive number regex validation fro non -0
believers, same as the above, except now it forbids numbers like -0
, -0.00000
, -0.0e0
, -0.00000e5
and -0e-6
:
对非0信众的负和正数正则表达式验证,与上述相同,但现在它禁止了-0、-0.00000、-0.0e0、-0.00000e5和-0e-6的数字:
/^(?!-0(\.0+)?(e|$))-?(0|[1-9]\d*)(\.\d+)?(e-?(0|[1-9]\d*))?$/i
测试这个正则表达式
Negative and positive number with invisible zero support regex validation, supports also the above positive and negative numbers, in addition numbers like -.1e3
, -.0e0
, -.0e-5
and -.1e-7
:
负数和正数,不可见的零支持regex验证,也支持上面的正数和负数,以及像-这样的数字。1 e3 -。0 e0 -。0 e-5 -.1e-7:
/^-?(0|[1-9]\d*)?(\.\d+)?(?<=\d)(e-?(0|[1-9]\d*))?$/i
测试这个正则表达式
Negative and positive number with the combination of non -0
believers and invisible zero believers, same as the above, but forbids numbers like -.0e0
, -.0000e15
and -.0e-19
:
负数和正数与非0信仰者和不可见的零信仰者结合,如上所述,但禁止数字如-。0 e0 -。0000年e15 -.0e-19:
/^(?!-0?(\.0+)?(e|$))-?(0|[1-9]\d*)?(\.\d+)?(?<=\d)(e-?(0|[1-9]\d*))?$/i
测试这个正则表达式
Numbers with Hexadecimal Representation
In many programming languages, string representation of hexadecimal number like 0x4F7A
may be easily cast to decimal number 20346
.
Thus, one may want to support it in his validation script.
The following Regular Expression supports only hexadecimal numbers representations:
在许多编程语言中,十六进制数字(如0x4F7A)的字符串表示形式可以很容易地转换为十进制数字20346。因此,您可能希望在验证脚本中支持它。下面的正则表达式仅支持十六进制数字表示:
/^0x[0-9a-f]+$/i
测试这个正则表达式
All Permutations
These final Regular Expressions, support the invisible zero numbers.
这些最后的正则表达式,支持不可见的零数。
Signed Zero Believers
/^(-?(0|[1-9]\d*)?(\.\d+)?(?<=\d)(e-?(0|[1-9]\d*))?|0x[0-9a-f]+)$/i
测试这个正则表达式
Non Signed Zero Believers
/^((?!-0?(\.0+)?(e|$))-?(0|[1-9]\d*)?(\.\d+)?(?<=\d)(e-?(0|[1-9]\d*))?|0x[0-9a-f]+)$/i
测试这个正则表达式
Hope I covered all number permutations that are supported in many programming languages.
Good luck!
希望我涵盖了许多编程语言支持的所有数字排列。好运!
Oh, forgot to mention, that those who want to validate a number includes a thousand separator, you should clean all the commas (,
) first, as there may be any type of separator out there, you can't actually cover them all.
But you can remove them first, before the number validation:
哦,忘了说,那些想要验证一个数字的人包括一千个分隔符,您应该首先清除所有的逗号(,),因为可能有任何类型的分隔符,您实际上不能全部覆盖它们。但你可以先删除它们,在数字验证之前:
//JavaScript
function clearSeparators(number)
{
return number.replace(/,/g,'');
}
#3
7
I had the same problem, but I also wanted ".25" to be a valid decimal number. Here is my solution using JavaScript:
我也有同样的问题,但我也想。25"是一个有效的小数。下面是我使用JavaScript的解决方案:
function isNumber(v) {
// [0-9]* Zero or more digits between 0 and 9 (This allows .25 to be considered valid.)
// ()? Matches 0 or 1 things in the parentheses. (Allows for an optional decimal point)
// Decimal point escaped with \.
// If a decimal point does exist, it must be followed by 1 or more digits [0-9]
// \d and [0-9] are equivalent
// ^ and $ anchor the endpoints so tthe whole string must match.
return v.trim().length > 0 && v.trim().match(/^[0-9]*(\.[0-9]+)?$/);
}
Where my trim() method is
我的trim()方法在哪里
String.prototype.trim = function() {
return this.replace(/(^\s*|\s*$)/g, "");
};
Matthew DesVoigne
马修DesVoigne
#4
5
I've tested all given regexes but unfortunately none of them pass those tests:
我已经测试了所有给定的regex,但不幸的是没有一个通过这些测试:
String []goodNums={"3","-3","0","0.0","1.0","0.1"};
String []badNums={"001","-00.2",".3","3.","a",""," ","-"," -1","--1","-.1","-0", "2..3", "2-", "2...3", "2.4.3", "5-6-7"};
Here is the best I wrote that pass all those tests:
下面是我写的通过所有这些测试的最好的:
"^(-?0[.]\\d+)$|^(-?[1-9]+\\d*([.]\\d+)?)$|^0$"
#5
2
Here is a great working regex for numbers. This accepts number with commas and decimals.
这是一个非常有用的数字正则表达式。它接受带有逗号和小数的数字。
/^-?(?:\d+|\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/
#6
1
Here is my regex for validating numbers:
下面是我的regex验证数字:
^(-?[1-9]+\\d*([.]\\d+)?)$|^(-?0[.]\\d*[1-9]+)$|^0$
Valid numbers:
有效数字:
String []validNumbers={"3","-3","0","0.0","1.0","0.1","0.0001","-555","94549870965"};
Invalid numbers:
无效的数字:
String []invalidNumbers={"a",""," ","-","001","-00.2","000.5",".3","3."," -1","--1","-.1","-0"};
#7
0
if you need to validate decimal with dots, commas, positives and negatives try this:
如果您需要使用点、逗号、正数和负数验证小数,请尝试以下方法:
Object testObject = "-1.5";
boolean isDecimal = Pattern.matches("^[\\+\\-]{0,1}[0-9]+[\\.\\,]{1}[0-9]+$", (CharSequence) testObject);
Good luck.
祝你好运。
#8
0
Below is the perfect one for mentioned requirement :
以下是最适合提出的要求:
^[0-9]{1,3}(,[0-9]{3})*(([\\.,]{1}[0-9]*)|())$
#9
0
/([0-9]+[.,]*)+/ matches any number with or withot coma or dots
/([0-9]+[.,]*)+/匹配任何有昏迷或没有圆点的数字
it can match
它可以匹配
122
122,354
122.88
112,262,123.7678
bug: it also matches 262.4377,3883 ( but it doesn't matter parctically)
bug:它还匹配262.4377和3883(但它没有多少区别)
#10
0
Try this code, hope it will help you
试试这段代码,希望它能对你有所帮助
String regex = "(\\d+)(\\.)?(\\d+)?"; for integer and decimal like 232 232.12