1

in this sentence if I want to track both IPs, I can scan the sentence until I find the first IP, capture the IP, the scan the sentence until second, all the characters besides the IP can be "discarded" since I do not need it. the following is the lines:

Gateway of last resort is 10.119.254.240 to network 10.140.0.0   
O E2 10.110.0.0 [160/5] via 10.119.254.6, 0:01:00, Ethernet2
**D 10.67.10.0 [200/128] via 10.119.254.244, 0:02:22, Ethernet2**
O E2 10.68.132.0 [160/5] via 10.119.254.6, 0:00:59, Ethernet2

the regex i have now is : [0-9]+(?:.[0-9]+){3}

this is part of the program to extract IP address that corresponded to it from the string to be used in a different section. the output for the program should be like this:

Protocol: EIGRP
Prefix: 10.67.10.0
AD/Metric:200/128
Next-Hop:10.119.254.244
Last Update:0:02:22
Outbound interface: Ethernet2

my idea is I thought I can extract the IP address and store it in the dictionary to be used later.

3 Answers3

0

If you would like to capture only the IP addresses from those string sequences, you could use the following regex:

>>> import re
>>> f = lambda s: re.findall(r'\d+\.\d+\.\d+\.\d+', s)
>>> f("Gateway of last resort is 10.119.254.240 to network 10.140.0.0")
['10.119.254.240', '10.140.0.0']
>>> f("O E2 10.68.132.0 [160/5] via 10.119.254.6, 0:00:59, Ethernet2")
['10.68.132.0', '10.119.254.6']

Disclaimer: I'd advise against this method of parsing IP addresses as strings can be highly polluted and lead to all sorts of edge cases. I'd recommend using an IP parsing library for any sort of production code.

ospahiu
  • 3,187
  • 2
  • 10
  • 22
  • the regex tat used in code .....is it only for those ip address tat i gave as sample or can be used for all the ip address – Calvin Rider Jan 23 '18 at 18:59
0

In your regex you have to escape the dot \. to match it literally or else the dot . would match any character.

[0-9]+(?:\.[0-9]+){3} or shorter \d+(?:\.\d+){3}

Note

While this matches the digits and dots in your example, this is not a regex to match an ip number. Maybe this page might be helpfull.

The fourth bird
  • 96,715
  • 14
  • 35
  • 52
0

You can match them in pairs, 1 pair per line, per match.
Group 1 is first ip, group 2 is second ip.

((?:[01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])(?:\.(?:[01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])){3})(?!\S).+?(?<!\S)((?:[01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])(?:\.(?:[01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])){3})

https://regex101.com/r/KUzfz0/1

Formatted

 (                             # (1 start), 1st IP
      (?:
           [01]? [0-9]? [0-9] 
        |  2 [0-4] [0-9] 
        |  25 [0-5] 
      )
      (?:
           \.
           (?:
                [01]? [0-9]? [0-9] 
             |  2 [0-4] [0-9] 
             |  25 [0-5] 
           )
      ){3}
 )                             # (1 end)
 (?! \S )                      # Wsp boundary ahead

 .+?                           # Stuff inbetween

 (?<! \S )                     # Wsp boundary behind
 (                             # (2 start), 2nd IP
      (?:
           [01]? [0-9]? [0-9] 
        |  2 [0-4] [0-9] 
        |  25 [0-5] 
      )
      (?:
           \.
           (?:
                [01]? [0-9]? [0-9] 
             |  2 [0-4] [0-9] 
             |  25 [0-5] 
           )
      ){3}
 )                             # (2 end)