我试图从用户那里收集一个ip并进行一些基本的错误检查和黑客攻击不必要的空白区域,但我遇到了一些我认为是我的正则表达式的问题。
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";
我知道正则表达式很简单,但它与1.1.1.1不匹配,因为我认为它应该
答案 0 :(得分:10)
变化:
if (/$serverIP = (\d+\.\d+\.\d+\.\d+)/){
为:
if ($serverIP =~ /(\d+\.\d+\.\d+\.\d+)/){