[英]Regular expression to match integers up to 9 digits
I want to create a regular expression where only numbers are allowed with max length 9 and no minimum length.我想创建一个正则表达式,其中只允许最大长度为 9 且没有最小长度的数字。 I came up with
\d{9}[0-9]
but it isn't working.我想出了
\d{9}[0-9]
但它不起作用。
You're close.你很近。 Try this:
尝试这个:
^\d{0,9}$
The ^ and $ match the beginning and the end of the text, respectively. ^ 和 $ 分别匹配文本的开头和结尾。
\d{0,9}
matches anywhere in the string, so d0000
would pass because it would match the 0000
even though there is ad in it, which I don't think you want. \d{0,9}
匹配字符串中的任何位置,因此d0000
会通过,因为即使其中有广告,它也会匹配0000
,我认为您不希望这样做。 That's why they ^$ should be in there.这就是为什么他们 ^$ 应该在那里。
Regular expressions can be tricky;正则表达式可能很棘手; what you've written does the following:
你所写的内容如下:
\d
- digit \d
- 数字\d{9}
- exactly 9 digits \d{9}
- 正好 9 位数字\d{9}[0-9]
- exactly 9 digits, followed by something between 0 and 9 \d{9}[0-9]
- 正好 9 位数字,后跟 0 到 9 之间的数字If you want no minimum limit of length, but a maximum length of 9, you probably want the following regular expression:如果您不需要最小长度限制,但最大长度为 9,您可能需要以下正则表达式:
\d{0,9}
- 0 to 9 digits \d{0,9}
- 0 到 9 位数字I think It should be 1 to 9 digits:我认为应该是1到 9 位数字:
^\d{1,9}$
It looks like you were close, try \d{0,9}
.看起来你很接近,试试
\d{0,9}
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.