带有小数点的可选逗号分隔数字的正则表达式 [英] Regex for optional comma separated number with decimal point

查看:41
本文介绍了带有小数点的可选逗号分隔数字的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找匹配以下内容的正则表达式

I'm after a regular expression which will match the following

1,245.30
24,235,235.50
12235235.60
235.50

小数点是强制性的,但我希望逗号是可选的.基于这个答案逗号分隔数的正则表达式我能够想出这个表达式这对于逗号分隔的数字非常有用.

The decimal is mandatory but I'd like the comma to be optional. Based off this answer Regex for Comma Separated Number I was able to come up with this expression which works great for the comma separated numbers.

(^\d{1,3}([,]\d{3})*([.]\d{2}){0,1}$)

但是,我无法将逗号设为可选.

However I'm having trouble making the commas optional.

推荐答案

嗯,只允许一个不带逗号的数字作为明确的替代方案:

Um, just allow a number without commas as an explicit alternative:

^(\d?\d?\d(,\d\d\d)*|\d+)(\.\d\d)?$

这篇关于带有小数点的可选逗号分隔数字的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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