使用逗号分隔符对数字进行正则表达式验证 [英] Regex validation for numbers with comma separator
本文介绍了使用逗号分隔符对数字进行正则表达式验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
需要一个正则表达式以使用逗号分隔符验证数字. 1,5,10,55 有效,但 1 ,,, 10 无效.
Need a regular expression to validate number with comma separator. 1,5,10,55 is valid but 1,,,,10 is not valid.
推荐答案
这应该做到:
^\d+(,\d+)*$
正则表达式非常简单:\d+
是第一个数字,后跟可选的逗号和更多数字.
The regex is rather simple: \d+
is the first number, followed by optional commas and more numbers.
您可能想将\s*
放在合适的位置,或者在验证之前删除所有空格.
You may want to throw in \s*
where you see fit, or remove all spaces before validation.
- 要允许负数,请将
\d+
替换为[+-]?\d+
- 要允许分数:将
\d+
替换为[+-]?\d+(?:\.\d+)?
- To allow negative numbers replace
\d+
with[+-]?\d+
- To allow fractions: replace
\d+
with[+-]?\d+(?:\.\d+)?
这篇关于使用逗号分隔符对数字进行正则表达式验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文