需要一个正则表达式的逗号分隔的号码列表 [英] Need a Regex for comma separated number list

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

问题描述

这应该是简单的正则表达式经验的作家,但我不写他们多,所以....

This should be simple for experienced regex writers, but I don't write them much, so....

我想对一个文本框输入验证一个C#MVC的形式,可能使用JavaScript或jQuery的。

I want to do input validation on a text box on a C# MVC form, possibly using javascript or jquery.

我想限制输入到逗号分隔的整数列表。该列表必须开始与多家> = 0,后跟一个逗号,然后重复这一模式。列表可以或可以不以逗号结束

I want to limit the input to a list of comma-separated integers. The list must start with a number >= 0, followed by a comma and then repeat this pattern. the list may or may not end with a comma:

1,2,444,5, - 通

1,2,444,5, - Pass

1,2,444,5 - 通

1,2,444,5 - Pass

,1,2,444,5, - 故障

,1,2,444,5, - Fail

,1,2,444,5 - 故障

,1,2,444,5 - Fail

1,2,444,5 - 故障

1,,2,444,5 - Fail

1,2,444,5 ,, - 故障

1,,2,444,5,, - Fail

我写的: ^([0-99]?)+ $ 和测试它在regexlib.com,它似乎工作,但测试仪返回2匹配,并且我不知道这意味着什么。由于它的失败案例失败之上,我认为这将是简单的输入验证是安全的。有没有更好的模式?

I wrote this: ^([0-99],?)+$ and tested it at regexlib.com and it seems to work, but the tester returns 2 matches, and I'm not sure what that means. Since it fails on the Failing cases above, I assume it would be safe for simple input validation. Is there a better pattern?

不太重要的问题:为什么允许它444时,范围为0-99

Less important question: Why does it allow 444 when the range is 0-99?

推荐答案

的范围内运营商仅在那里指定字符的ASCII,而不是数字的范围。试试这个:

the range operator is there only to specify range of ASCII chars, not numbers. Try this instead:

^([0-9] + ,?)+ $

这篇关于需要一个正则表达式的逗号分隔的号码列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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