Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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!

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

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

Because IP addresses are not written as 123.456.789., but rather 123.456.789
What about the 4th octet?

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.

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.

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.

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

![]() |
![]() |
![]() |

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