Computing.Net > Forums > Programming > Perl regex for IP address

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Perl regex for IP address

Reply to Message Icon

Name: ShaqDiesel
Date: May 27, 2008 at 21:18:09 Pacific
OS: winXP
CPU/Ram: amd64/1Gb
Product: custom
Comment:

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!



Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: May 27, 2008 at 21:51:19 Pacific
Reply:

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


0

Response Number 2
Name: FishMonger
Date: May 27, 2008 at 22:01:25 Pacific
Reply:

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


0

Response Number 3
Name: FishMonger
Date: May 27, 2008 at 22:04:06 Pacific
Reply:

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

What about the 4th octet?


0

Response Number 4
Name: Razor2.3
Date: May 27, 2008 at 22:09:25 Pacific
Reply:

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.


0

Response Number 5
Name: FishMonger
Date: May 27, 2008 at 22:15:33 Pacific
Reply:

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


0

Related Posts

See More



Response Number 6
Name: ShaqDiesel
Date: May 28, 2008 at 14:16:41 Pacific
Reply:

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.


0

Response Number 7
Name: FishMonger
Date: May 28, 2008 at 15:23:49 Pacific
Reply:

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+/


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Perl regex for IP address

Perl: local IP info w/o knowing www.computing.net/answers/programming/perl-local-ip-info-wo-knowing/3953.html

IP Address www.computing.net/answers/programming/ip-address/2728.html

Extract IP address from txt file www.computing.net/answers/programming/extract-ip-address-from-txt-file/13912.html