sud*_*hkr 5 python regex string ip parsing
我有点陷入从字符串中提取 IPv4 地址的困境。
我的输入字符串和约束如下:
0.0.0.0至255.255.255.255this is an ip&this is an ip 200.100.2.32200.100.2.32 is an ip| 输出 :['200.100.2.32']200.100.2.32is an ip| 输出 :[] the ip is 200.100.2.32| 输出 :['200.100.2.32']the ip is200.100.2.32| 输出 :[]the ip is 200.100.2.32 and it is ipv4| 输出 :['200.100.2.32']the ip is 200.100.2.32and it is ipv4| 输出 :[]200.100.2.32 100.50.1.16| 输出 :['200.100.2.32', '100.50.1.16']200.100.2.32.100.50.1.16| 输出 :[]我正在尝试为上述情况构建一个正则表达式,它们看起来相当简单,并且我无法合并所有正则表达式检查。
我一直在参考这些链接上的答案:Link1、Link2、Link3
有人可以帮助我朝正确的方向前进吗?总结一下:
0.0.0.0到255.255.255.255代码
def find_ip(str) :
ip_pattern = re.compile('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\s') # need to strengthen the regex here
ip = re.findall(ip_pattern, str)
return ip
Run Code Online (Sandbox Code Playgroud)
正则表达式:
(?:^|\b(?<!\.))(?:1?\d\d?|2[0-4]\d|25[0-5])(?:\.(?:1?\d\d?|2[0-4]\d|25[0-5])){3}(?=$|[^\w.])
Run Code Online (Sandbox Code Playgroud)
匹配的一个例子。
| 归档时间: |
|
| 查看次数: |
8184 次 |
| 最近记录: |