简体   繁体   中英

Regex for IP address is not matching

I was attempting to collect an ip from a user and do some basic error checking and hacking off the unnecessary white space, but I am running into some issues with what I assume to be my regular expression.

my $serverIP;

print "Enter vSphere server's IP\n"; 
while(<>){
  $serverIP = $_;
  print "DEBUG <$serverIP>\n";
  if (/$serverIP = (\d+\.\d+\.\d+\.\d+)/){
    print "Match found $1\n";
    $serverIP = $1;
    last;
  }
  else{
    print "Not an IP\n";
    print "Please enter a valid IP\n";
  }
}   
print "I found $serverIP!\n";

I know the regex is simple but it does not match 1.1.1.1 as I assume it should

Change:

  if (/$serverIP = (\d+\.\d+\.\d+\.\d+)/){

to:

  if ($serverIP =~ /(\d+\.\d+\.\d+\.\d+)/){

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM