There have been a number of pages around T'internet on regexps for determining an IP number, and I needed a slight variation.... which has led me to a really natty regexp for the base use-case:
chomp $ip; my $quad = '([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])'; if ($string =~ /^(${quad}\.){3}${quad}$/) { ## have valid IP }
All previous examples have been long strings defining each quad separately... I use two simple tricks:
oh... and ${quad} is just a way of ensuring that the right variable name is used... a-la $foo = "${bar}barella" looks for $bar and appends barella to it
In my case, I wanted to try to guess between a (partial) IP number, a hostname, or some other string (a name) - so I have:
# $q contains the string to test my $quad = '([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])'; given ($q) { when ( # Is it an IP? # matches 127 or 127. or 127.0 or 127.0. or... # does not match any quad above 255, or 127.0.0.1. # as the fifth dot fails the match /^((${quad}\.){0,2}${quad}\.?|(${quad}\.){3}${quad})$/ ) { $type = 'ip'; } ## end when ( ...) when (/\w+\.\w+/) { # Is it a domain name? # words.words $type = 'url' } default { $type = 'name' } # it must be a name then... } ## end given
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Checking for a valid IP number
by rustic (Monk) on Jun 11, 2012 at 13:07 UTC | |
|
Re: Checking for a valid IP number
by mbethke (Hermit) on Jun 30, 2012 at 19:41 UTC |