正则表达式允许字符串仅包含数字 0 - 9 并将长度限制为 45

发布于 2024-09-27 18:02:02 字数 319 浏览 1 评论 0原文

我正在尝试创建一个正则表达式,使字符串仅包含 0-9 作为字符,并且长度必须至少为 1 个字符且不超过 45。例如,00303039 会匹配,而 039330a29 则不会。

到目前为止,这就是我所拥有的,但我不确定它是否正确,

[0-9]{1,45} 

我也尝试过,

^[0-9]{45}*$

但这似乎也不起作用。我对正则表达式不太熟悉,所以任何帮助都会很棒。谢谢!

I am trying to create a regex to have a string only contain 0-9 as the characters and it must be at least 1 char in length and no more than 45. so example would be 00303039 would be a match, and 039330a29 would not.

So far this is what I have but I am not sure that it is correct

[0-9]{1,45} 

I have also tried

^[0-9]{45}*$

but that does not seem to work either. I am not very familiar with regex so any help would be great. Thanks!

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

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

发布评论

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

评论(8

审判长 2024-10-04 18:02:02

快到了,您所需要的只是开始锚点 (^) 和结束锚点 ($):

^[0-9]{1,45}$

\d 是字符类的缩写<代码>[0-9]。您可以将其用作:

^\d{1,45}$

锚点强制模式匹配整个输入,而不仅仅是其中的部分


您的正则表达式 [0-9]{1,45} 查找 1 到 45 位数字,因此像 foo1 这样的字符串也会匹配,因为它包含 1

^[0-9]{1,45} 查找 1 到 45 位数字,但这些数字必须位于输入的开头。它匹配 123 但也匹配 123foo

[0-9]{1,45}$ 查找 1 到 45 位数字,但这些数字必须位于输入的结束。它匹配 123 但也匹配 foo123

^[0-9]{1,45}$ 查找 1 到 45 位数字,但这些数字必须是两者在输入的开始和结束处,实际上应该是整个输入。

You are almost there, all you need is start anchor (^) and end anchor ($):

^[0-9]{1,45}$

\d is short for the character class [0-9]. You can use that as:

^\d{1,45}$

The anchors force the pattern to match entire input, not just a part of it.


Your regex [0-9]{1,45} looks for 1 to 45 digits, so string like foo1 also get matched as it contains 1.

^[0-9]{1,45} looks for 1 to 45 digits but these digits must be at the beginning of the input. It matches 123 but also 123foo

[0-9]{1,45}$ looks for 1 to 45 digits but these digits must be at the end of the input. It matches 123 but also foo123

^[0-9]{1,45}$ looks for 1 to 45 digits but these digits must be both at the start and at the end of the input, effectively it should be entire input.

无声静候 2024-10-04 18:02:02

第一个匹配字符串中任意数量的数字(也允许其他字符,即:“039330a29”)。第二个仅允许 45 位数字(且不少于)。因此,只需取两者的优点即可:

^\d{1,45}$

其中 \d[0-9] 相同。

The first matches any number of digits within your string (allows other characters too, i.e.: "039330a29"). The second allows only 45 digits (and not less). So just take the better from both:

^\d{1,45}$

where \d is the same like [0-9].

孤独陪着我 2024-10-04 18:02:02

如果您不想从零开始,请使用此正则表达式:

^[1-9]([0-9]{1,45}$)

如果您不介意从零开始,请使用:

^[0-9]{1,45}$

Use this regular expression if you don't want to start with zero:

^[1-9]([0-9]{1,45}$)

If you don't mind starting with zero, use:

^[0-9]{1,45}$
拥抱我好吗 2024-10-04 18:02:02

您可能需要两种尝试的组合:

^[0-9]{1,45}$

A combination of both attempts is probably what you need:

^[0-9]{1,45}$
二手情话 2024-10-04 18:02:02

codaddict 提供了正确的答案。至于你所尝试的,我将解释为什么他们没有成功:

  • [0-9]{1,45} 几乎就在那里,但它匹配 1 - 至 45 位数字的字符串即使它出现在另一个包含其他字符的较长字符串中。因此,您需要 ^$ 将其限制为完全匹配。

  • ^[0-9]{45}*$ 匹配精确的 45 位数字字符串,重复 0 次或任意次数 (*)。这意味着字符串的长度只能是 0 或 45 的倍数(90、135、180...)。

codaddict has provided the right answer. As for what you've tried, I'll explain why they don't make the cut:

  • [0-9]{1,45} is almost there, however it matches a 1-to-45-digit string even if it occurs within another longer string containing other characters. Hence you need ^ and $ to restrict it to an exact match.

  • ^[0-9]{45}*$ matches an exactly-45-digit string, repeated 0 or any number of times (*). That means the length of the string can only be 0 or a multiple of 45 (90, 135, 180...).

尹雨沫 2024-10-04 18:02:02

^[0-9]{1,45}$ 正确。

^[0-9]{1,45}$ is correct.

出于某些安全原因,Rails 不喜欢使用 ^ 和 $ ,可能最好使用 \A 和 \z 来设置字符串的开头和结尾

Rails doesnt like the using of ^ and $ for some security reasons , probably its better to use \A and \z to set the beginning and the end of the string

酷到爆炸 2024-10-04 18:02:02

对于这种情况,也可以使用单词边界 (\b) 代替起始锚点 (^) 和结束锚点 ($):

\b\d{1,45}\b

\b 是 \w 和\W(非单词字符),或位于字符串的开头或结尾。

For this case word boundary (\b) can also be used instead of start anchor (^) and end anchor ($):

\b\d{1,45}\b

\b is a position between \w and \W (non-word char), or at the beginning or end of a string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文