正则表达式允许空格(对于电话号码正则表达式)
我有这个正则表达式:
preg_match("#^([0-9]+)$#", $post['telephone'])
who 只允许数字(对于法语类型的电话号码,所以 0123456789),但我想允许空格。例如允许这种类型的字符串:“01 23 45 67 89”。
你能帮我吗?
I have this regex:
preg_match("#^([0-9]+)$#", $post['telephone'])
who allow only numbers (for a phone number in the French type so 0123456789) but I would like to allow spaces. For example allow this type of string: "01 23 45 67 89".
Can you please help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
例如,如果您想要恰好 8 位数字,但仍允许任意数量的空白字符,则以下内容很好:
^(?:\s*\d){8}$
或者,如果您也想允许破折号:
^(?:\s*-*\s*\d){8}$
If you want to have exactly 8 digits, e.g., but still allow for arbitrary number of whitespace characters, the following is great:
^(?:\s*\d){8}$
Or, if you want to allow dashes too:
^(?:\s*-*\s*\d){8}$
如果字符串中的任何位置都可以有空格,那么很简单,只需将其添加到字符类中即可,
但这将允许开头有 5 个空格。
会更复杂一点。以两位数字开头,然后是一个可选组,以可选空格开头,后跟至少 1 位数字。该组可以重复 0 次或多次。
这将匹配
If it is OK to have spaces anywhere in your string then it is simple, just add it to your character class
but this will allow 5 spaces in the beginning.
would be a bit more sophisticated. Starts with two digits, then a optional group starting with an optional space followed by at least 1 digit. this group can be repeated 0 or more times.
This would match
这个删除了除数字、空格和破折号之外的任何内容。
您想只是验证(匹配和中止)还是清理,尝试清理并继续?
This one strips out anything but digits, spaces and a dash.
Do you want to just validate (matches and abort) or cleanse, try and clean up and carry on?
刚刚遇到了同样的问题,如果有人遇到这个问题,我创建了这个正则表达式:
演示: https: //regex101.com/r/aFIwp7/1
它将涵盖手机号码和非手机号码的所有法语选项。
Just had the same issue, if anyone comes across this issue, I've created this regex :
Demo: https://regex101.com/r/aFIwp7/1
It will cover all french options for mobile numbers and non mobile numbers.
这个怎么样?
How about this?