逗号分隔数或正数的正则表达式 [英] Regex for Comma Separated Number or Just Number
本文介绍了逗号分隔数或正数的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是来自上一个问题的后续内容。要求已更改,我正在寻找有关以逗号分隔的数字或没有逗号的数字的正则表达式的帮助。下面是示例输入以及一些失败的尝试。
This is something of a follow up from a previous question. The requirements have changed, and I'm looking for some help with coming up with regex for either a comma separated number or a number with no commas. Sample input is below, along with some failed attempts.
我们将不胜感激。
可接受的输入:
1,234,567
1234567
不可接受的输入:
1234,567
1,234567
某些尝试失败:
^(\d{1,3}(?:[,]\d{3})*)|((\d)*)$
^\d{1,3}((?:[,]?\d{3})*|(?:[,]\d{3})*)$
最初的成功:
^\d{1,3}(?:[,]\d{3})*$
推荐答案
只需在末尾添加或一个或多个数字即可:
Just add an "or one or more digits" to the end:
^(?:\d{1,3}(?:[,]\d{3})*|\d+)$
我认为您第一次就拥有它几乎是正确的,只是没有正确匹配所有括号。
I think you had it almost right the first time and just didn't match up all your parentheses correctly.
这篇关于逗号分隔数或正数的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文