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()
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
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 .
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.