0

How could I match the following where the IP address can change:

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

Thanks in advance!

2 Answers2

3

You can try the below code, change the string to restrict only specific texts.

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";
}
Srihari Karanth
  • 2,067
  • 2
  • 24
  • 34
  • If there can be nothing but an IP address, this is fine. The OP doesn't even seem to care about capturing the IP. You do not need to escape colon `:`, it does not have a special meaning in regex. – simbabque Mar 16 '17 at 16:44
1

A general regex for the ipv4 (no port) would be
(?<!\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)

Explained

 (?<! \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 )
  • I'm not sure you have to worry about not-matching 256.256.256.256 – stevesliva Mar 16 '17 at 19:36
  • @stevesliva - Yeah ? Even if the world consisted of nothing but digits and dots ? –  Mar 18 '17 at 05:16
  • "How could I match the following where the IP address can change." This complicated regex is nice, and the explanation is good-- if you're worried about the input not being an IP. But it doesn't actually seem to fully answer the question. – stevesliva Mar 18 '17 at 17:16
  • @stevesliva - and what's your definition of _fully_? I could mark it as a duplicate if you think there are other answers to IP regex questions. –  Mar 18 '17 at 17:59