简体   繁体   English

如何从字符串中提取 IP 地址

[英]How to extract IP address from the string

I am trying to extract from the IP address from the string我正在尝试从字符串中提取 IP 地址

import re
stri_ = '''192.168.1.1,192.168.1.2/32,192.168.1.5-192.168.1.7,reject,
reject,192.168.1.1/32,reject
172.168.1.4-172.168.1.4,reject
'''
ip_addr_with_range = re.findall('([0-9+-reject]+)\,(reject)*',stri_)
ip_addr_without_range = re.findall('/([0-9\-reject]+)*',stri_)
for i in ip_addr_with_range :
      print (i[0])

My Out我的出局

192.168.1.1,192.168.1.2/32,192.168.1.5-192.168.1.7,reject,reject,192.168.1.1/32
172.168.1.4-172.168.1.4

Desired Out想出去

 ip_addr_with_range
[192.168.1.1,192.168.1.2/32,192.168.1.5,192.168.1.7,192.168.1.1/32,172.168.1.4,172.168.1.4]


ip_addr_without_range
 [192.168.1.1,192.168.1.2,192.168.1.5,192.168.1.7,192.168.1.1,172.168.1.4,172.168.1.4]

This should work.这应该有效。

ip_addr_with_range = re.findall('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\/?\d{0,}', stri_)
ip_addr_without_range = re.findall('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', stri_)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM