php regex:电话号码7到12位数字可能包含连字符或空格 [英] php regex: phone number 7-12 digits may contain hypen or space

查看:143
本文介绍了php regex:电话号码7到12位数字可能包含连字符或空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了以下链接中包含7-12位数字的电话号码的解决方案,其中可能包含空格或连字符.第一个和最后一个字符必须是数字.

I tried the solution for phone numbers with 7-12 digits that may contain spaces or hypens in the following link. The first and last character has to be a number.

匹配7的正则表达式-12位数字;可能包含空格或连字符

但是,我对正则表达式不太了解.

However, i'm not understanding the regex well.

$phone_pattern="/^\d(?:[-\s]?\d){6,11}$/";

:"在这里是什么意思?

what does the ":" mean here?

此正则表达式如何排除6到11个字符之间的连字符和空格?

How is this regex able to exclude the hypens and spaces from the 6 to 11 characters?

非常感谢帮助

推荐答案

您是否尝试过像regexpal这样的测试引擎(还有其他可用的引擎),我经常使用它来针对表达式测试各种字符串,以确保它们的行为符合预期

Have you tried a testing engine like regexpal (there are others available also) I frequently use this to test various strings against expressions to make sure they are behaving as expected.

我的理解是,在这种情况下,:不是单独行动,而是与?

My understanding is that in this case the : is not acting alone it is acting in conjunction with the ?

在这种情况下,?并不意味着它是修饰语之前的零或一倍,这给该组赋予了新的含义,并与修饰符:一起关闭capture表示您希望这是一个非捕获的组表达式.

The ? in this circumstance does not mean Preceding zero or one times it is a modifier meaning give this group a new meaning which in conjunction with the modifier : turn off capture means you want this to be a non capturing group expression.

其效果是,将表达式的一部分放置在()内时,默认情况下会引起捕获,但?:会将其关闭.

The effect that this has is that when placing part of an expression inside () it by default causes capture but ?: switches this off.

因此(?:[-\ s]?\ d)成为非捕获组表达式.

Thus (?:[-\s]?\d) becomes a non capturing group expression.

注释捕获组与反向引用一起使用,大多数正则表达式引擎最多支持9个反向引用.

Note captured groups are used with back references and most regex engines support up to 9 back references.

因此,删除捕获可加快匹配过程,并允许您选择不回溯该组,从而将9个引用中的一个保存为您确实要回溯的组.

So removing capture speeds up the matching process and allows you to elect not to refer back to that group saving one of your 9 references for a group that you really do want to refer back to.

这篇关于php regex:电话号码7到12位数字可能包含连字符或空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆