正则表达式允许字符串仅包含数字 0 - 9 并将长度限制为 45
我正在尝试创建一个正则表达式,使字符串仅包含 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
快到了,您所需要的只是开始锚点 (
^
) 和结束锚点 ($
):\d
是字符类的缩写<代码>[0-9]。您可以将其用作:锚点强制模式匹配整个输入,而不仅仅是其中的部分。
您的正则表达式
[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 ($
):\d
is short for the character class[0-9]
. You can use that as: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 likefoo1
also get matched as it contains1
.^[0-9]{1,45}
looks for 1 to 45 digits but these digits must be at the beginning of the input. It matches123
but also123foo
[0-9]{1,45}$
looks for 1 to 45 digits but these digits must be at the end of the input. It matches123
but alsofoo123
^[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.第一个匹配字符串中任意数量的数字(也允许其他字符,即:“039330a29”)。第二个仅允许 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:
where
\d
is the same like[0-9]
.如果您不想从零开始,请使用此正则表达式:
如果您不介意从零开始,请使用:
Use this regular expression if you don't want to start with zero:
If you don't mind starting with zero, use:
您可能需要两种尝试的组合:
A combination of both attempts is probably what you need:
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...).^[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
对于这种情况,也可以使用单词边界 (\b) 代替起始锚点 (^) 和结束锚点 ($):
\b
是 \w 和\W(非单词字符),或位于字符串的开头或结尾。For this case word boundary (\b) can also be used instead of start anchor (^) and end anchor ($):
\b
is a position between \w and \W (non-word char), or at the beginning or end of a string.