cppcoder cppcoder - 1 year ago 136
Perl Question

Perl regular expression to match an IP address

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";
if($ip =~ /(\d{1-3}\.\d{1-3}\.\d{1-3}\.\d{1-3}\:\d{1-5})/)
print "$1\n";

I wanted to just extract the IP address, not do any validation.

Answer Source

Change {1-3} to {1,3} same for {1-5} -> {1,5}

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download