[RegEx] regular expressions match IP addresses and domain names

Source: Internet
Author: User

Reprinted: http://blog.csdn.net/seawave/article/details/1520988

After reading several pages of mastering regular expression, I tried to write a regular expression that could match the IP string. I did not write a few lines of code to determine whether it was easy.

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:

Match Regular Expression Description
0 ~ 9 /D Single digit
10 ~ 99 [1-9]/d Double digits
100 ~ 199 1/D/d The number of three digits with one hundred bits
200 ~ 249 2 [0-4]/d The number of three digits, the number of hundred digits is 2, and the number of digits is 0 ~ 9
250 ~ 255 25 [0-5] The number of three digits, the number of hundred digits is 2, the number of ten digits is 5, and the number of single digits is 0 ~ 5

Write as a regular expression, that is: (/d | ([1-9]/d) | (1/D/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/d) | ([1-9]/D) |/d )(/. (25 [0-5]) | (2 [0-4]/d) | (1/D/d) | ([1-9]/D) |/D) {3}

Press:

  • A number with a high value of 0 such as 061 cannot be matched.
  • It's too troublesome. It's easier to write a small piece of code for parsing.

 

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] {}) + /.?

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.