请帮我制作正十进制数的正则表达式,其中有 0、1 或 2 位小数.它必须允许逗号和点.例如它必须允许:
Please help me make regular expression for positive decimal number with 0, 1 or 2 decimal places. It must allow comma and dot. For example it must allow:
0,01 0.01 0,1 1 1.1 1,11但不允许:
-1 0.0 0,00 .01 0 1,111 1.111我有这个/(^\d*(?:\.|\,)?\d*[1-9]+\d*$)|(^[1-9]+\d*(?:\.|\,)\d*$)/但我找不到如何禁止超过 2 个小数位.
I have this /(^\d*(?:\.|\,)?\d*[1-9]+\d*$)|(^[1-9]+\d*(?:\.|\,)\d*$)/ but I can`t find how to disallow more than 2 decimal places.
更新男人,我必须拒绝 0.0、0 等等.
UPDATE Men, I must reject 0.0, 0 and etc.
推荐答案编辑 2:现在完全不允许 0,0.0 等
这匹配小数点前至少一位数字,后跟一个可选的小数位,后跟 0-2 位数字.
This matches at least one digit before the decimal place, followed by an optional decimal place, followed by 0-2 digits.
负前瞻查找绝对零的任何风味并防止匹配.
^(?!0*[.,]0*$|[.,]0*$|0*$)\d+[,.]?\d{0,2}$这是原始的正则表达式,因此您需要针对您的语言对其进行适当的转义.(例如,在某些语言中,您需要将 \ 斜线加倍为 \\.
This is the raw regex, so you'll need to escape it appropriately for your language. (For example, in some languages you need to double the \ slashes as \\.
/^(?!0*[.,]0*$|[.,]0*$|0*$)\d+[,.]?\d{0,2}$/更多推荐
带有 0、1 或 2 个小数位的正十进制数的正则表达式
发布评论