The general North American phone number format is:
999-999-9999
Or 999x999x9999 (X represents any character)
Match with character groups:
[0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]
Abbreviated character group:
\d\d\d-\d\d\d-\d\d\d\d
Including the selected characters:
^(\(\d{3}\)|^\d{3}[.-]?)?\d{3}[.-]?\d{4}$
The first ^ indicates that the phone number will appear at the starting position of a line.
Left brackets {start character of the capture group
\ (Represents the left bracket itself (Escape Character)
\ D matches a digit
The {3} After \ D is a quantizer, indicating that it matches three digits.
\} Match the right brace itself (Escape Character)
| Indicates selection, that is, multiple options are selected
^ Match the starting position of a row
\ D matches a digit
{3} indicates the quantifiers matching three digits.
[.-]? Match an optional dot or hyphen
The ending character of the capture group.
? Indicates that the group is optional, that is, the prefix in the group is optional.
\ D matches a digit
{3} indicates the quantifiers matching three digits.
[.-]? Match another optional dot or hyphen
\ D matches a digit
{4} is a quantizer that matches four digits.
$ Match the end position of a row
Next we will talk about a regular expression that matches the North American phone number:
^\(?(?:\d{3})\)?[.-]?(?\d{3})[.-]?(?:\d{4})$
Where (? : XXXX) indicates a non-capturing group. A non-capturing group does not store its content in the memory, resulting in higher performance.
Match the regular expression of the email address:
^([\w-.!#$%&'*-+=/?^_{|}~]+)@((?:\w+\.)+)(?:[a-zA-Z]{2,4})$
Predefined character set:
\ W matches a word character, equivalent to [a-zA-Z0-9]
\ W matches a non-word character [\ ^ \ W]
\ D indicates matching a number [0-9]
\ D matches a non-digit [^ \ D]
\ S matches a blank character [<space> \ t \ r \ n \ f \ v]
\ S matches a non-blank character [^ \ s]
Quantifiers:
* Match the first character 0 or unlimited times
+ Match the first character once or infinitely
? Match the first character 0 or 1 time
{M} matches the previous character m times
{M, n} matches the previous character M-N times
Boundary
^ Match the start of a string. In multiline mode, match the beginning of each line.
$ Match the end of a string. In multiline mode, China matches the end of each row.
\ A only matches the start of a string
\ Z only matches the end of the string
\ B matches \ W and \ W
\ B match [^ \ B]
[] Character Set
{} Generally refers to quantifiers.
() Indicates a group.
(? :) Generally, it indicates a non-capturing group.