带有 0、1 或 2 个小数位的正十进制数的正则表达式 [英] Regular expression for positive decimal number with 0, 1 or 2 decimal places

查看:30
本文介绍了带有 0、1 或 2 个小数位的正十进制数的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮我制作正十进制数的正则表达式,其中有 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 个小数位的正十进制数的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆