Search code examples
phpregexphone-number

php regex: phone number 7-12 digits may contain hypen or space


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.

Regular expression to match 7-12 digits; may contain space or hyphen

However, i'm not understanding the regex well.

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

what does the ":" mean here?

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

Help help is highly appreciated


Solution

  • 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 ?

    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.

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

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

    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.