正则表达式需要 10 位数字而不考虑空格
我需要一个正则表达式将数字限制为 10,即使有空格也是如此。
例如,它允许 06 15 20 47 23
与 0615204723
相同。
我尝试过:^\d{10}$
,但是如何忽略空格?
此外,号码应以 06
或 07
开头。 (编者注,最后一个要求来自OP的评论。)
I need a regex to limit number digits to 10 even if there are spaces.
For example it allows 06 15 20 47 23
the same as 0615204723
.
I tried: ^\d{10}$
, but how do I ignore spaces?
Also, the number should start with 06
or 07
. (Editor's note, the last requirement was from comments by the OP.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这将在大多数正则表达式系统中完成。 (您需要说明您使用的语言!)
但假设您真的只想要这个数字,那么请继续将其分为两个步骤。
例如,在 JavaScript 中:
This will do it in most regex systems. (You need to state what language you are using!)
But assuming you really only want the number, then go ahead and break it into 2 steps.
For example, in JavaScript:
你可以这样做:
You can do:
稍微好一点:
其中 \s 是任何空格,\d 是数字。这允许前导以及中固定和尾随任何一组空格。
Slightly preferable:
Where \s is any whitespace and \d is a digit. This allows for leading as well as infixed and trailing any group of spaces.
我遇到了同样的问题,我注意到如果我将每个数字与任意数量的空格填充正面和背面,我就得到了我正在寻找的解决方案。在此示例中,忽略所有空格
在更复杂的示例中,这会忽略所有空格、破折号、圆括号和空格。
在这里测试它们,它将为您提供给定系统的代码:
http://www.myregextester.com/index.php
I had the same problem, I noticed if I grouped each digit with any number of spaces padding the front and back, I got the solution I was looking for. In this example, ignores all spaces
In a more complicated example, this ignored all spaces, dashes, round braces and spaces.
Test them out here, it will provide you with the code for your given system:
http://www.myregextester.com/index.php
这应该允许数字周围有前导和尾随空格,并匹配 0 到 10 位数字。
This should allow leading and trailing spaces around digits, and match 0 to 10 digits.