Ip address regular expression, ip address Regular Expression

Source: Internet
Author: User

Ip address regular expression, ip address Regular Expression

Regular expressions are used to format and match characters, and a single command is used to complete the judgment.

 

IP address format: x. x

X indicates 0 ~ Number of 255

In three cases

A. 250-255: features: the number of three digits, the number of hundred digits is 2, the number of digits is 5, and the number of digits is 0 ~ 5. You can use a regular expression as follows: 25 [0-5]
B. 200-249: features: the number of three digits, the number of hundred digits is 2, and the number of digits is 0 ~ 4. The single digit is 0 ~ 9, which can be written as: 2 [0-4] \ d
C. 0-199: this can be further split, Which is simpler and clearer.
C. a. 0-9: features: one-digit, with a single digit ranging from 0 to 9 ~ 9. The regular expression can be written as \ d.
C. B. 10-99: feature: two-digit, one to ten ~ 9, with a single digit ranging from 0 ~ 9. The regular expression can be written as [1-9] \ d.
C. c. 100-199: features: three digits, 1 for hundreds of digits, and 0 for ten digits ~ 9, with a single digit ranging from 0 ~ 9. The regular expression can be written as 1 \ d {2}

Therefore, the regular expression 0-99 can be written into [1-9]? \ D, then 0-199 can be written as (1 \ d {2}) | ([1-9]? \ D), So 0 ~ The Regular Expression of 255 can be written as (25 [0-5] | 2 [0-4] \ d | (1 \ d {2 }) | ([1-9]? \ D )))
Finally, you can use {3} to repeat the previous three segments and use the same match again in section 4th to obtain the regular expression of the IP Address:

^((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))))$
 
The meaning of each symbol, refer to MSDN
 
Reference source webpage

IP address Regular Expression

Back to jiemy_1987
Your expression is too much. Think about it again.
If your expression has more than 6 numbers, an error is returned. If each segment does not have 1 or 2, an error is returned.
Try it.
Var str = '6. 6.6.6 ';
Var str2 = '55. 55.55.55 ';

Below is my:
$ Patten = '/^ ([1-9] | ([1-9] \ d) | (1 \ d) | (2 ([0-4] \ d | 5 [0-5]) \.) (d | ([1-9] \ d) | (1 \ d) | (2 ([0-4] \ d | 5 [0-5]) \.) {2} ([1-9] | ([1-9] \ d) | (1 \ d) | (2 ([0-4] \ d | 5 [0-5]) $ /';

It's a little bloated, but it can definitely be used.
Ip: 1.0.0.1 to 255.255.255.255
Each segment cannot start with "0"
---------------------------------------
There are so many people copying this year.
Modified
$ Patten = '/^ ([1-9] | ([1-9] \ d) | (1 \ d) | (2 ([0-4] \ d | 5 [0-5]) \.) ([1-9] | ([1-9] \ d) | (1 \ d) | (2 ([0-4] \ d | 5 [0-5]) \.) {2} ([1-9] | ([1-9] \ d) | (1 \ d) | (2 ([0-4] \ d | 5 [0-5]) $ /';
I'm sorry.

How to Write a regular expression for an IP address?

The Regular Expression of the IP Address:
^ (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1 -9] {1} [0-9] {1} | [1-9]) \\. (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1- 9] {1} [0-9] {1} | [1-9] | 0) \\. (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1- 9] {1} [0-9] {1} | [1-9] | 0) \\. (25 [0-5] | 2 [0-4] [0-9] | [0-1] {1} [0-9] {2} | [1- 9] {1} [0-9] {1} | [0-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.