需要一个正则表达式来匹配不能全零的可变长度数字字符串

发布于 2024-07-13 05:57:52 字数 436 浏览 7 评论 0原文

我需要验证表单上的输入。 我期望输入是 1 到 19 位数字之间的数字。 输入也可以从零开始。 但是,我想验证它们并不全为零。 我有一个正则表达式,可以确保输入是数字,并且数字在 1 到 19 之间。

^\d[1,19]$

但我不知道如何包括检查整个字符串不全为零。 我尝试了这个

^(![0]{1,19})(\d[1,19])$

,但它在 0000000000000000001 上失败,因为它允许可变数量的零。

如何检查整个字符串不为零?

谢谢。

我正在尝试在 ASP.NET RegularExpressionValidator 中执行此操作,因此我希望有一个表达式。 我还有其他选择,所以如果不能做到这一点,我也不会运气不好。

I need to validate an input on a form. I'm expecting the input to be a number between 1 to 19 digits. The input can also start with zeros. However, I want to validate that they are not all zeros. I've got a regex that will ensure that the input is numeric and between 1 and 19 numbers.

^\d[1,19]$

But I can't figure out how to include a check that the entire string is not all zeros. I tried this

^(![0]{1,19})(\d[1,19])$

but it fails on 0000000000000000001 because it's allowing a variable number of zeros.

How do I check that the entire string is NOT zeros?

Thanks.

I'm trying to do this in a ASP.NET RegularExpressionValidator so I was hoping for a single expression. I have other options, so I'm not out of luck if this can't be done.

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

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

发布评论

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

评论(4

不知在何时 2024-07-20 05:57:52

^(?!0+$)\d{1,19}$

^(?!0+$)\d{1,19}$

长途伴 2024-07-20 05:57:52

只需进行否定前瞻:

(?!^0+$)(^\d{1,19})

这在 Perl 中工作得很好。

Just do a negative lookahead:

(?!^0+$)(^\d{1,19})

This works fine in Perl.

想念有你 2024-07-20 05:57:52

(?!0+$) 是一个前瞻指令。 这 ?! 是负向先行命令,用于搜索 1 个或多个 0 到字符串末尾。 如果匹配,则使用这些字符,留下 \d{1,19} 的常规数字搜索。

Boost Perl 正则表达式 对 Boost 认可的 perl 正则表达式进行了很好的讨论。

(?!0+$) is a lookahead directive. The ?! is the negative lookahead command to search for 1 or more 0's to the end of the string. If that matches, then the characters are consumed, leaving the regular digit search of \d{1,19}.

Boost Perl Regexp has a good discussion of perl regexp as recognized by Boost.

锦欢 2024-07-20 05:57:52

你不需要正则表达式

            ulong test;
        string number = "1234567890123456789";
        if (ulong.TryParse(number, out test) && (test < 9999999999999999999ul) && (test > 0))
            Console.WriteLine(test);

you don't need RegEx for that

            ulong test;
        string number = "1234567890123456789";
        if (ulong.TryParse(number, out test) && (test < 9999999999999999999ul) && (test > 0))
            Console.WriteLine(test);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文