Regular expression for Zero value or null value
c开发者_开发技巧an someone tell me the regular expression which will check if value is 0.0 or 00.00 and that it is a positive value.
This should work:
/^0*(\.0+)$?/
0 -> true
.0 -> true
.000 -> true
.001 -> false
.01 -> false
1.0000 -> false
0.000000 -> true
1.1100 -> false
Note: this regex assumes there are no constructs like +0.00
; if you need something like that, use this: /^\+?0*(\.0+)$?/
. Also, if you ask me, you should the parseFloat(str) === 0.0
equivalent for your language.
/^\+?0+(\.0+)?$/
should do the trick.
(I know 0's can't be positive or negative, but I feel it's good to check for a positive sign just in case)
This will match exactly 0.0 and 00.00 as requested. But it's a good idea to check on an undefined amount of zero's as described in other answers.
/^0{1,2}\.0{1,2}$/
Can't really say this is any better than what's already presented, but it's a different approach:
$r = "^[^-0.]"
"0.0" -notmatch $r
"00.00" -notmatch $r
"-1" -notmatch $r
"1" -notmatch $r
True
True
True
False
精彩评论