带有 0、1 或 2 个小数位的正十进制数的正则表达式

编程入门 行业动态 更新时间:2024-10-28 02:24:36
本文介绍了带有 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 个小数位的正十进制数的正则表达式

本文发布于:2023-11-10 23:03:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1576706.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:小数位   十进制数   正则表达式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!