Regular Expression (IP and domain name)

Source: Internet
Author: User

 

1.

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.
Obviously, 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, it is composed of google and com. At last, there can be a point that represents the root domain (in the specification, the last point is the complete domain name, however, it is generally considered that a domain name with more than two names is also a complete domain name, even if it is not followed ).
The regular expression that matches the complete Domain Name:
[A-zA-Z0-9] [-a-zA-Z0-9] {} (\. [a-zA-Z0-9] [-a-zA-Z0-9] {}) + \.?

 

2.

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:
(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.