-1
import re

ip = "10.20.30.40 this is an ip. 20.30.40.50.100 this is not ip"

def checkip(ip):
    pat = "\d+\.\d+\.\d+\.\d+"
    ips = re.findall(pat, ip)
    print(ips)
    
checkip(ip)

output: ['10.20.30.40', '20.30.40.50'] --> here second one is not an ip. but it it picking part of the string.

When I tried the pattern as "^\d+.\d+.\d+.\d+$" I'm not getting empty list.

I don't want second string to be picked, as it is not an ip address. I want to grep only ip address. Not sure what I'm missing and how to debug this. Can someone please help here.

Ram
  • 27
  • 5
  • Can you elaborate on the last line of your question? “*Second one is not a string*” What does this exactly mean in this context? It is in fact a string, no? – esqew Aug 12 '21 at 17:23
  • @esqew I have a feeling that he ment "Second one is not valid ip adress string", not sure tho... – TDiblik Aug 12 '21 at 17:26
  • @esqew I said that second one is not an ip address. ip address contains only four comma separated octets. As second string means "20.30.40.50.100" which is having 5 octets. I want to ignore that. – Ram Aug 12 '21 at 17:29
  • @TDiblik yes you are right. Thanks for adding the comment – Ram Aug 12 '21 at 17:30
  • 1
    "*I said that second one is not an ip address*" Yes, you did earlier in your question. But the last line here literally says "*Second one is not a string*", can you explain that or edit it to be a bit more clear on what you intended to convey here? – esqew Aug 12 '21 at 17:36

2 Answers2

1

If you're reasonably certain the IP addresses you'd like to extract from the broader ip string will be preceded and proceeded by whitespace characters (as in your example) or appear at the beginning/end of the broader string, you can use the following pattern to match them:

((?<=\s)|^)((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)((?=\s)|$)

Regex101 | H/T to this SO answer for the pattern to only match "valid" addresses

If you need additional assurance that the extracted IP or IPs are strictly valid according to the specification, you can use a module like ipaddress to validate they are spec-compliant.

esqew
  • 42,425
  • 27
  • 92
  • 132
  • Thanks for the comment. I got some clue from your comment. Thank you very much. – Ram Aug 13 '21 at 04:56
-1

Thanks all for responding.

I couldn't find solution with the above patterns. But I found it myself. The following is working fine. I can extract all the ip like strings then later I can validate them separately.

pat = "\d+.\d+.\d+.\d+\S+"

it working fine. I'm getting the following output.

['10.20.30.40', '20.30.40.50.100']

Ram
  • 27
  • 5