Matches the regular expressions of IP addresses and domain names.

Source: Internet
Author: User

Matches the regular expressions of IP addresses and domain names.

A complete domain name consists of the root domain, top-level domain, level 2, level 3 ...... Domain names are separated by dots. Each domain name consists of letters, numbers, and minus signs (the first letter cannot be a minus sign). It is case-insensitive and cannot exceed 63 characters in length.

A separate name can be matched by a regular expression [a-zA-Z0-9] [-a-zA-Z0-9] {}, and a complete domain name contains at least two names (such as google.com, composed of google and com ), finally, there can be a point indicating the root domain (in the specification, the last point is the complete domain name, but it is generally considered that the domain name containing more than two names is also a complete domain name, even if it is not followed by a dot ).

The regular expression that matches the complete Domain Name:

Copy codeThe Code is as follows:
[A-zA-Z0-9] [-a-zA-Z0-9] {} (\. [a-zA-Z0-9] [-a-zA-Z0-9] {}) + \.?

An IP string consisting of four segments, each of which is 0 ~ A decimal point. For example, 61.139.2.69 is a legal IP string.

If the regular expression is written as \ d {1, 3} (\. \ d {1, 3}) {3}, It is not responsible because it can match an invalid IP string such as 300.400.555.666.

To match a 0 ~ There are several matching methods for the number between 255:

Regular Expression matching description

0 ~ 9 \ d single digit
10 ~ 99 [1-9] \ d two digits
100 ~ 199 1 \ d the number of three digits with one hundred bits
200 ~ 249 2 [0-4] \ d three-digit number, bits are 2, and 10 bits are 0 ~ 9
250 ~ 255 25 [0-5] three digits, digits are 2, 10 digits are 5, and one digit is 0 ~ 5

Written as a regular expression, that is, (\ d | ([1-9] \ d) | (1 \ d) | (2 [0-4] \ d) | (25 [0-5]), but when such a regular expression matches strings like 254, it will match 2, 5, and 4 respectively to get three matches, if the expected results are not met, the correct method is to reverse the order to (25 [0-5]) | (2 [0-4] \ d) | (1 \ d) | ([1-9] \ d) | \ d), because in the (xxx | yyy) Matching Behavior, It is searched from left to right.

The complete regular expression is:

 Copy codeThe Code is as follows:
(25 [0-5]) | (2 [0-4] \ d) | (1 \ d) | ([1-9] \ d) | \ d )(\. (25 [0-5]) | (2 [0-4] \ d) | (1 \ d) | ([1-9] \ d) | \ d) {3}

Press:

A number with a high value of 0 such as 061 cannot be matched.

Therefore, the top (1. Part) method is the correct full version, and the (2. Part) method is 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.