OT: PERL Question

Nov 13, 2015 5 Replies

What is the most compact way to express this IP range with a PERL regular expression:


114.108.128.0 - 114.108.191.255

Thanks! ...Jim Thompson



-- | James E.Thompson | mens | | Analog Innovations | et | | Analog/Mixed-Signal ASIC's and Discrete Systems | manus | | San Tan Valley, AZ 85142 Skype: skypeanalog | | | Voice:(480)460-2350 Fax: Available upon request | Brass Rat | | E-mail Icon at

formatting link
| 1962 | I love to cook with wine. Sometimes I even put it in the food.


here's an ugly one (they all are) from an IP range regex generator

^114\.108\.(1(2[8-9]|[3-8][0-9]|9[0-1]))\.([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))$

Yup. That's what I was coming up with... ugly ;-) ...Jim Thompson

| James E.Thompson | mens | | Analog Innovations | et | | Analog/Mixed-Signal ASIC's and Discrete Systems | manus | | San Tan Valley, AZ 85142 Skype: skypeanalog | | | Voice:(480)460-2350 Fax: Available upon request | Brass Rat | | E-mail Icon at http://www.analog-innovations.com | 1962 | I love to cook with wine. Sometimes I even put it in the food.

Convert it to 32-bit binary and test the top 18 bits (discard 14):

use Net::IP; my $ip = new Net::IP("114.108.128.0"); print $ip->binip(), "\n";

=> 01110010011011001000000000000000

Clifford Heath.

as as CIDR range 114.108.129.0/19 but I guess you can't use that.

as regex, assuming input is four-part ip-addresses only.

114\.108\.1(2[89]|[3-8]\d|9[01])\.\d+
\_(?)_

Since in the context it will be used you don't mind if it catches malformed IP addresses as well you could swap \d for "." saving 2.

114\.108\.1(2[89]|[3-8].\9[01])\..+

I prefer yours though which is exact on IP addresses. (but he did ask for the shortest regex)

Regards, Martin Brown

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required