Php regular expression (1 ): verification mobile phone number This article introduces the character group, quantifiers, start/end position of the string, group, selection structure, reverse reference, and name in the regular expression by gradually improving a regular expression for verification mobile phone number group.
1. basic verification
Verify that the string is an 11-digit number.
Expression
[1, 0123456789] {11}
Or [0-9] {11}
Or \ d {11}
Knowledge Point
Character Group:In the regular expression, [...] is represented by square brackets. Character groups indicate characters that may appear at the same position.
For example, [0123456789] indicates matching any of the digits 0123456789; [0123abc] indicates matching any of the digits 0123 and abc.
Range representation of character groups:The character Group uses a hyphen ([...-...]) to indicate characters in a range.
For example, [a-z] indicates matching any one of all lower-case English letters; [a-zA-Z] indicates matching all lower-case English letters and one of the upper-case English letters; [0-9] indicates matching any of the 0123456789 values.
It should be noted that the default range is the characters between the ACS Ⅱ code of the starting character and the ACS Ⅱ code of the ending character..
Character Group notation:For some common character groups, regular expressions specify some note symbols to represent them.
\ D all numbers, that is, [0-9]
All non-numbers of \ D are mutually exclusive with \ d.
\ W all word characters (characters, numbers, and underscores), that is, [0-9a-zA-Z _]
All non-word characters of \ W are mutually exclusive with \ W.
All blank characters in \ s, including spaces, tabs, carriage returns, line breaks, and other blank characters
All non-blank characters in \ S are mutually exclusive with \ s
Quantifiers:Quantifiers indicate the number of times a modified object (such as a character or character group) may appear.
The general form of quantifiers is {m, n} (comma, followed by no space), indicating that the number of occurrences of the character (or character Group) it modifies is greater than or equal to m, less than or equal to n times. In particular
{M} indicates that the modifier object can only appear m times;
{0, n} indicates that the modified object can appear at most n times and at least 0 times;
{M,} indicates that the modified object appears at least m times.
2. is the length really only 11?
Observe the code in the following gif and you can see that when the input string is a number of 15 characters, it can also match the first 11 digits. Even if the input character is abcd180123412341234, it can match 11 numbers.
This is because the above regular expression means "matching 11 numbers", soAs longIn the input string11 consecutive numbersThe matching is successful. To verify that the entered string is only the mobile phone number, use the start position ^ and end position $ of the string in the regular expression.
Expression
Knowledge Point
Some symbols in a regular expression match positions rather than text.Anchor(Anchor ). ^ And $ are two of them.
^ The matching position is the start position of the string.
$ The matched position is the end position of the string
3 more rigorous verification
We all know that common mobile phone numbers in China start with 130-139,150-153-155, 159-180, 182, 170-, and 185-189. In addition, there are also and 176-178 mobile phone numbers. The expression we obtained in the previous section does not validate the beginning of the mobile phone.
Expression
^ 1 (3 [0-9] | 5 [012356789] | 8 [0256789] | 7 [0678]) \ d {8} $
Knowledge Point
Group:You can use parentheses (...) in a regular expression (...) indicates a group (subexpression). In this way, in addition to returning all the matched content in the matching results, each sub-expression is also returned. For example, in the result of executing the expression in, the 0th elements in the array are the values matched by the entire regular expression, and the 1st elements are the values matched by regular expressions in parentheses.
preg_match('/^1(3[0-9]|5[012356789]|8[0256789]|7[0678])\d{8}$/', '18012341234', $arr);print_r($arr);/*Array( [0] => 18012341234 [1] => 80)*/
Select structure:The subexpressions in parentheses (...) are separated by vertical bars to indicate different choices. the entire regular expression in parentheses can match any choice.
For example, (3 [0-9] | 5 [012356789] | 8 [0256789] | 7 [0678]) the value can be 3 [0-9], 5 [012356789], 8 [0256789], or 7 [0678.
4 icing on the cake
Sometimes, there is a-symbol in the middle of the phone number, which is in the form of 180-1234-1234. for example, the current iPhone will automatically convert the phone number to this format.
Based on the knowledge introduced so far, you can write the following regular expressions to be compatible with the 180-1234-1234 format:
^ 1 (3 [0-9] | 5 [012356789] | 8 [0256789] | 7 [0678]) -{0, 1} \ d {4}-{0, 1} \ d {4} $
-{0, 1} indicates that the character-can appear once or not. this is a quantizer we have learned before. In fact, in a regular expressionCommon quantifiersA special note is also stipulated:
? Equivalent to {0, 1}, which can appear 0 times or 1 time
+ Equivalent to {1,}, appears more than or equal to 1
* Equivalent to {0,}, appears more than or equal to 0 times
Therefore, the above regular expression is equivalent
^ 1 (3 [0-9] | 5 [012356789] | 8 [0256789] | 7 [0678])-? \ D {4 }-? \ D {4} $
However, in addition to matching 18012341234 and 180-1234-1234, the above expressions can also match 180-12341234 and 1801234-1234.
If you only want to match the 18012341234 and 180-1234-1234 forms, you can useReverse reference:
^ 1 (3 [0-9] | 5 [012356789] | 8 [0256789] | 7 [0678]) (-?) \ D {4} \ 2 \ d {4} $
The above \ 2 is a reverse reference, which matches the content matched by the second parentheses. The reverse reference is in the form of \ num, which references the content matched by the previous group in the regular expression.
In the above regular expression, we use \ 2 for reverse reference, but \ 1 is useless. can we ignore those groups that are not used? In the regular expressionNon-capturing groupThis requirement can be met:
^ 1 (? : 3 [0-9] | 5 [012356789] | 8 [0256789] | 7 [0678]) (-?) \ D {4} \ 1 \ d {4} $
The above (? : 3 [0-9] | 5 [012356789] | 8 [0256789] | 7 [0678]) is a non-capturing group. The non-capturing form is (? :...). If a non-capturing group is used, no matching results are found in the matched results.
The group reference is based on the number of the subexpression. when the regular expression is complex or has too many numbers, it is very painful to find out the number of each group. Therefore, regular expressions provideNaming Group:
^ 1 (? : 3 [0-9] | 5 [012356789] | 8 [0256789] | 7 [0678]) (? P -?) \ D {4 }(? P = separato) \ d {4} $
In the above regular expression (? P -?) Is the name group. The naming Group is in the form (? P ...), Reference the naming Group (? P = name.
5. Summary
So far, a robust regular expression for mobile phone number verification is complete. Although the function is simple, it involves many knowledge points in regular expressions.