Articles

Perl regex for IP address

May 27, 2008 at 21:18:09
Specs: winXP, amd64/1Gb

Why does "([\d]{3}\.){3}." work? The dot is a special character, so to match it literally don't I need to escape it? Unfortunately, escaping it will not match any ip addresses in a text file!

See More: Perl regex for IP address

Report •


#1
May 27, 2008 at 21:51:19

The dot is a special character, so to match it literally don't I need to escape it?
Yes.

Unfortunately, escaping it will not match any ip addresses in a text file!
Because IP addresses are not written as 123.456.789., but rather 123.456.789


Report •

#2
May 27, 2008 at 22:01:25

Why are you putting a character class within a character class? \d is the shortcut for the [0-9] character class.

Your regex is incomplete and makes the false assumption that the first 3 octets will always be 3 digits. That assumption may work for your needs in this case, but it's a poor assumption and the regex will fail for an IP such as: 10.125.10.125 or even a more common address such as 192.168.1.1

You didn't show how you're using the regex, but I assume you're using it like this:
$ip =~ /([\d]{3}\.){3}./

Your regex matches 3 consecutive groups of 3 digits followed by a . then the unescaped . matches any single character. Which means your regex will match 192.168.125.P


Report •

#3
May 27, 2008 at 22:04:06

Because IP addresses are not written as 123.456.789., but rather 123.456.789

What about the 4th octet?


Report •

Related Solutions

#4
May 27, 2008 at 22:09:25

What about the 4th octet?
Alright, it's official. It's past my bedtime and I can't really see too straight.

But the point still stands.


Report •

#5
May 27, 2008 at 22:15:33

There are several modules on cpan that are specifically designed for this purpose, but if you prefer to roll your own regex, then use one that's more robust.

http://aspn.activestate.com/ASPN/Co...


Report •

#6
May 28, 2008 at 14:16:41

it doesnt have to be an ip address, just numbers formatted like this ###.###.###.#

My main question was why escaping the last "." won't give me a match.


Report •

#7
May 28, 2008 at 15:23:49

If you escape the last '.', then the regex will attempt to match:
###.###.###..

The '.' inbetween the numbers is already specified in ([\d]{3}\.) which makes the last '.' unwanted/redundant.

If you want to remain with a simple regex, you probably want this:
/(\d{3}\.){3}\d+/


Report •


Ask Question