
Kenosis
User
Dec 4, 2013, 11:15 AM
Post #7 of 7
(2098 views)
|
Your IP-matching regex needs a little attention. For example:
use warnings; use strict; while (<DATA>) { if (m/\d{1,2}.\d{1,3}.\d{1,3}.\d{1,3}/) { print "Yes: $_"; } else { print "No : $_"; } } __DATA__ 127.0.0.1 9995.333.777.000 255.255.255.0 abc.def.ghi.jkl 10_376_14.0 1234567890 Output:
Yes: 127.0.0.1 Yes: 9995.333.777.000 Yes: 255.255.255.0 No : abc.def.ghi.jkl Yes: 10_376_14.0 Yes: 1234567890 Note that it's matching too much. Also, the leading m is used only when ^ (match from the beginning) or $ (denoting the end) is used in the regex, so that can be removed:
/\d{1,2}.\d{1,3}.\d{1,3}.\d{1,3}/ Didn't you mean /\d{1,2} for the first octet, like all the rest? Also, the period in your regex isn't escaped \., so it will match any character except a newline. Making these changes, you get:
/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/ with an output used in the script above of:
Yes: 127.0.0.1 Yes: 9995.333.777.000 Yes: 255.255.255.0 No : abc.def.ghi.jkl No : 10_376_14.0 No : 1234567890 This is better, but you don't want 9995.333.777.000. You want to match an IP that's surrounded by a word boundary, and that's represented by \b. So, we now have:
/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/ with an output of:
Yes: 127.0.0.1 No : 9995.333.777.000 Yes: 255.255.255.0 No : abc.def.ghi.jkl No : 10_376_14.0 No : 1234567890 This is matching only what we want matched. Now, you can go one step further here, using the notation in \d{1,3}. Notice that the IP addresses have a repeating pattern: (nnn.) x 3 (assuming three digits in the octets), following by nnn as the last octet. You can represent that in a regex as follows:
/\b(?:\d{1,3}\.){3}\d{1,3}\b/ And this generates the same output as the last regex, but it's just a bit shorter. Hope this helps!
(This post was edited by Kenosis on Dec 4, 2013, 11:17 AM)
|