需要一个正则表达式来表示逗号分隔的数字列表

发布于 2024-10-12 20:24:12 字数 508 浏览 2 评论 0原文

对于经验丰富的正则表达式编写者来说,这应该很简单,但我写得不多,所以......

我想对 C# MVC 表单上的文本框进行输入验证,可能使用 javascript 或 jquery。

我想将输入限制为逗号分隔的整数列表。该列表必须以数字 >= 0 开头,后跟逗号,然后重复此模式。该列表可以或可以不以逗号结尾:

1,2,444,5, - 通过

1,2,444,5 - 通过

,1,2,444,5, - 失败

,1,2,444,5 - 失败

1,,2,444,5 -失败

1,,2,444,5,, - 失败

我写了这个: ^([0-99],?)+$ 并在 regexlib.com 上对其进行了测试,它似乎可以工作,但是测试仪返回 2 个匹配项,我不确定这意味着什么。由于它在上面的失败案例中失败,我认为对于简单的输入验证来说是安全的。有更好的模式吗?

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

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

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

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, - Pass

1,2,444,5 - Pass

,1,2,444,5, - Fail

,1,2,444,5 - Fail

1,,2,444,5 - Fail

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

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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风铃鹿 2024-10-19 20:24:12

范围运算符仅用于指定 ASCII 字符的范围,而不是数字。试试这个:

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

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

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

囍孤女 2024-10-19 20:24:12

你的正则表达式是错误的:它说“从字符串的开头,匹配一个或多个组,使得该组由数字0到9组成(其他9是多余的),可能后面跟逗号。直到最后”。

这显然不是你想要的。您需要这个:

^\d+(?:,\d+)*$

它匹配:“从字符串开头匹配一个或多个数字,可选地后跟由逗号和 1 组成的组或更多数字,直到字符串末尾”。这些组是非捕获组,因此您最多可以进行一场比赛。

Your regexp is wrong: It says "from the beginning of the string, match one or more groups such that the group is made of digits 0 to 9 (other 9 is redundant), maybe followed by comma. Up to the end".

This is clearly not what you want. You need this:

^\d+(?:,\d+)*$

It matches: "from the beginning of the string match at one or more digits, optionally followed by groups consisting of comma followed by one or more digits, up to the end of the string". The groups are non-capturing one, so you can have at most one match.

橘虞初梦 2024-10-19 20:24:12

^(([0-9],?)+)$ 或 ^([0-9],?)+$/ 取决于重用

测试

my %a=qw(1,2,444,5,  Pass
1,2,444,5  Pass
,1,2,444,5, Fail
,1,2,444,5  Fail
1,,2,444,5  Fail
1,,2,444,5,, Fail
);

while(my ($k,$v)=each(%a)) {
    $vv = ($k =~ m/^(([0-9],?)+)$/) ? "Pass" : "Fail";
    print "$k $v $vv\n";
};

^(([0-9],?)+)$ or ^([0-9],?)+$/ depending on reuse

Test

my %a=qw(1,2,444,5,  Pass
1,2,444,5  Pass
,1,2,444,5, Fail
,1,2,444,5  Fail
1,,2,444,5  Fail
1,,2,444,5,, Fail
);

while(my ($k,$v)=each(%a)) {
    $vv = ($k =~ m/^(([0-9],?)+)$/) ? "Pass" : "Fail";
    print "$k $v $vv\n";
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文