在正则表达式中匹配已知主机警告

时间:2017-03-16 16:16:48

标签: regex perl

如何匹配IP地址可以更改的以下内容:

Warning: Permanently added '100.124.61.161' (RSA) to the list of known hosts.

提前致谢!

2 个答案:

答案 0 :(得分:3)

您可以尝试以下代码,更改字符串以仅限制特定文本。

if($string =~ m/Warning: Permanently added '(.*?)' \(RSA\) to the list of known hosts\./)
{
   print "Match Successful, IP address: $1\n";
}
else
{
   print "String did not match\n";
}

答案 1 :(得分:1)

ipv4(无端口)的一般正则表达式为
(?<!\d)(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])(?:\.(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])){3}(?!\d)

解释

 (?<! \d )
 (?:
      \d                      # 0 - 9
   |  [1-9] \d                # 10 - 99
   |  1 \d{2}                 # 100 - 199
   |  2 [0-4] \d              # 200 - 249
   |  25 [0-5]                # 250 - 255
 )
 (?:
      \.
      (?:
           \d 
        |  [1-9] \d 
        |  1 \d{2} 
        |  2 [0-4] \d 
        |  25 [0-5] 
      )
 ){3}
 (?! \d )