Regular Expression-Learn from matching North American phone numbers and email addresses

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.