假设我有3个字符串:
str1 = 'Hello my name is Ben and my IP address is 127.1.1.1'
str2 = 'Hello my name is Folks and my IP address is 1.2.3.4.5'
str3 = 'Hello all, ip addresses: 1.2.3.4.5, 127.1.2.1, 127.2.1.2'
我想得到以下输出:
find_ip(str1) #['127.1.1.1']
find_ip(str2) #[]
find_ip(str2) #['127.1.2.1', '127.2.1.2']
P.S this StackOverflow post的解决方案不回答此问题。
答案 0 :(得分:2)
以下正则表达式将从0.0.0.0
到255.255.255.255
的IP进行匹配,而不在句号或数字之前或之后:
(?<![\.\d])(?:[0-9]\.|1\d?\d?\.|2[0-5]?[0-5]?\.){3}(?:[0-9]|1\d?\d?|2[0-5]?[0-5]?)(?![\.\d])
演示:https://regex101.com/r/UUCywc/3
编辑:为避免匹配以负数开头的IP(例如-127.2.1.2
),并允许使用001.001.001.001
之类的IP,请使用:
(?<![-\.\d])(?:0{0,2}?[0-9]\.|1\d?\d?\.|2[0-5]?[0-5]?\.){3}(?:0{0,2}?[0-9]|1\d?\d?|2[0-5]?[0-5]?)(?![\.\d])
演示:https://regex101.com/r/UUCywc/6
完整的Python实现:
import re
str1 = 'Hello my name is Ben and my IP address is 127.1.1.1'
str2 = 'Hello my name is Folks and my IP address is 1.2.3.4.5'
str3 = 'Hello all, ip addresses: 1.2.3.4.5, 127.1.2.1, 127.2.1.2'
def find_ip(test_str):
regex = re.compile(r"(?<![-\.\d])(?:0{0,2}?[0-9]\.|1\d?\d?\.|2[0-5]?[0-5]?\.){3}(?:0{0,2}?[0-9]|1\d?\d?|2[0-5]?[0-5]?)(?![\.\d])")
return regex.findall(test_str)
print(find_ip(str1)) #['127.1.1.1']
print(find_ip(str2)) #[]
print(find_ip(str3)) #['127.1.2.1', '127.2.1.2']
答案 1 :(得分:0)
请尝试使用以下正则表达式。它将获得以127开头的IP地址,后跟3组带点的数字。
re.findall( r'127+(?:\.[0-9]+){3}', str1 )
re.findall( r'127+(?:\.[0-9]+){3}', str2 )
re.findall( r'127+(?:\.[0-9]+){3}', str3 )
结果:
['127.1.1.1']
[]
['127.1.2.1', '127.2.1.2']