I have written this code, but it does not work. Can someone point out the issue?
sub match_ip()
{
my $ip = "The IP address is 216.108.225.236:60099";
if($ip =~ /(\d{1-3}\.\d{1-3}\.\d{1-3}\.\d{1-3}\:\d{1-5})/)
{
print "$1\n";
}
}
EDIT: I wanted to just extract the IP address, not do any validation.
http://metacpan.org/pod/Regexp::Common::net
If you extract an IP address that is not an IP address... you are not extracting the right thing.
This might help:
Output:
You can also use the following regex to make sure that the quad's aren't bigger then 255, it also "reuses" the digit matching instead of copypasting it 4 times.
It uses a few features from perl regex matching (man perlre):
In the spirit of TIMTOWTDI here is another: the
Regexp::Common::net
portion ofRegexp::Common
may have regexen that you desire.