如何使用正则表达式使用可选的百分比符号验证小数? [英] How validate decimal with optional percentage symbol using Regex?

查看:27
本文介绍了如何使用正则表达式使用可选的百分比符号验证小数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为问题的标题,我需要使用以下值验证正则表达式:(最多 2 个小数位和 9 个整数),带有可选的百分比符号.

As the title of the question, I need to validate regex using the following values​​: (Maximum 2 decimal places, and 9 integers) with an optional percent symbol.

有效:

10%
 0%
 1111111.12%
 15.2%
 10
 2.3

无效:

 .%
 12.%
 .02%
 %
 123456789123.123

我尝试过:

^[0-9]{0,9}([\.][0-9]{0,2})\d[\%]{0,1}?$

但它不能如我所愿.

推荐答案

试试这个

^\d{1,9}(\.\d{1,2})?%?$

我对 rubular 进行了测试,对于您的示例集来说没问题.

I tested over rubular and it is ok for your set of example.

这篇关于如何使用正则表达式使用可选的百分比符号验证小数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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