0

I am trying to find an IP pattern or any similar pattern in a string. for example:

Text_1 = "Hello this ip is valid: 123.22.33.22 , but!" #expect 123.22.33.22

Text_2 = "this could be the second valid ip: 323.123.22.33.22 , but!" #expect 323.123.22.33.22

Text_3 = "third pattern is: 01.002.33.222 , but!" #expect 01.002.33.222

Text_4 = "fourth pattern is: 332.332.222 , but!" #expect 332.332.222

In all cases I need to extract all numbers which are separate by dots and later on evaluate if they are potentially valid or not.

I had a look to this question and this question, but all have some issues!

This is what I found but does not work perfectly as it can't catch the string longer than 4-digit:

import re
re.search(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', s).group()
Community
  • 1
  • 1
Braiano
  • 335
  • 6
  • 26

3 Answers3

0

How about: map(int, “167.78.2.99”.split(“.”))) (split by . And try to convert each to an integer) and check for type error, check if len() is 4 and check for each element 0<=el<256.

Sorry for no code, not at my computer

Lars
  • 1,869
  • 2
  • 14
  • 26
  • Thanks for your answer, but how did you extract that 4-digits/5-digits/3-digits numbers from my provided strings? – Braiano Dec 21 '19 at 09:26
  • Oops, did not read the question well enough, i guess you could use answer 1, or if you do not like re, split on spaces, go over each word and strip it of anything but numbers and use the code i gave. – Lars Dec 23 '19 at 08:24
0

If you want any sequence of numbers and dots, try this:

# Find a number, then one or more ".numbers"
re.search(r'(\d+)(\.(\d+))+', Text_2).group()

It gives:

'323.123.22.33.22'

Note: After extracting candidates, you can check it with the regex provided by this answer.

Jaideep Shekhar
  • 808
  • 2
  • 7
  • 21
0

This finds exact ip addresses:

^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$

https://regex101.com/r/3biYkC/1

Update, i added a word boundry to it, it seems to work:

\b(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}\b

https://regex101.com/r/3biYkC/2

oppressionslayer
  • 6,942
  • 2
  • 7
  • 24