Regular expression _ regular expression that matches the IP address and domain name

Source: Internet
Author: User
Tags regular expression

A complete domain name, by the root domain, top-level domain, level two, three ... Domain name composition, each level of domain name separated by point, each domain name by letter, number and minus sign (the first letter can not be a minus), case-insensitive, not more than 63.

A single name can be matched by a regular expression [a-za-z0-9][-a-za-z0-9]{0,62}. And the complete domain name includes at least two names (such as Google.com, made up of Google and COM, you can finally have a point that represents the root domain (in the specification, the last point is the full domain name, but generally think that include more than two names of the domain name is also a complete domain name, even if there is no point behind it).

Regular expression that matches the full domain name:

Copy Code code as follows:

[A-za-z0-9] [-a-za-z0-9] {0,62} (\. [A-za-z0-9] [-a-za-z0-9] {0,62}) +\.?

An IP string, composed of four paragraphs, each segment is a 0~255 number, segments and segments separated by a decimal point, such as 61.139.2.69 is a legitimate IP string.

If the regular expression is written as \d{1,3} (\.\d{1,3}) {3}, it is no doubt irresponsible because it can match an illegal IP string such as 300.400.555.666.

To match the number between a 0~255, there are several ways to match it, and here's one:

Match Regular Expression description

0~9 \d Single digit
10~99 [1-9]\d two digits
100~199 1\d\d Hundred is 1 three digits
200~249 2[0-4]\d Three digits, the Hundred is 2, 10 is 0~9
250~255 25[0-5] Three digits, the Hundred is 2, 10 is 5, the single-digit is 0~5

Written as a regular expression, that is: (\d| ( [1-9]\d) | (1\d\d) | (2[0-4]\d) | (25[0-5]), but such regular expressions match 2, 5, 4, and get 3 matches to match 254 of such strings, and the correct approach is to reverse the order ((25[0-5)) | ( 2[0-4]\D) | (1\d\d) | ([1-9]\d) |\d), because it is searched from left to right in this matching behavior (xxx|yyy).

The complete regular expression is:

 

Copy Code code as follows:

((25[0-5]) | (2[0-4]\d) | (1\d\d) | ([1-9]\d) |\d) (\. ( (25[0-5]) | (2[0-4]\d) | (1\d\d) | ([1-9]\d) |\d)) {3}

By:

Numbers like 061 with a high of 0 cannot be matched.

Therefore, the top (1 part) of the writing is the correct full version, (2) The writing is more one-sided

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.