0

I am trying to match the pattern of an IP address. what I have is [0-999].[0-999].[0-999].[0-999] what test data are: 10.37.255.1 , 1.1.1.10 , and 2.2.3.3.3. Unfortunately it picks up the first 2 data sets as well as the first 4 numbers of the third data. what can I do to exclude the third data set in the return?

enter image description here

PChao
  • 417
  • 2
  • 5
  • 17
  • First, ip address don't use `0 - 999` they go up to `255`. Nevertheless, a regex is not a great way to do this ([see also](https://stackoverflow.com/questions/5284147/validating-ipv4-addresses-with-regexp) ).. You are probably better off using the builtin [ip address module](https://docs.python.org/3/library/ipaddress.html) – Mark Oct 24 '22 at 02:59

1 Answers1

1

You could surround your regex pattern by whitespace lookarounds:

inp = "These are my IP addresses 10.37.255.1 and 1.1.1.10 and this is my serial and ID number 4.1.2, 2.2.3.3.3"
ips = re.findall(r'(?<!\S)\d+(?:\.\d+){3}(?!\S)', inp)
print(ips)  # ['10.37.255.1', '1.1.1.10']

Side note: [0-999] does not match the numbers 1 to 999. Instead, it is identical to [1-9], which matches any single number from 1 to 9.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • I know they are just trying to find numbers separated by dots, but this is not a good solution for anyone finding this in the future. It matches invalid ip address like `980.37.255.1` and `10090.0.0.1` – Mark Oct 24 '22 at 03:09
  • @Mark My answer will extract all valid 4 part IP addresses. As for _validating_ those IP addresses, the OP may consider the options given in your comment. – Tim Biegeleisen Oct 24 '22 at 03:12