600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > python使用正则表达式抽取文件中的IP地址

python使用正则表达式抽取文件中的IP地址

时间:2021-07-20 19:43:26

相关推荐

python使用正则表达式抽取文件中的IP地址

python使用正则表达式抽取文件中的IP地址

IP正则

r(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})

有效IP

((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)

#

# importing the moduleimport re# opening and reading the filewith open( est2.txt) as fh:string = fh.readlines()# declaring the regex pattern for IP addressespattern =pile(\((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\)# initializing the list objectsvalid =[]invalid=[]# extracting the IP addressesfor line in string:line = line.rstrip()result = pattern.search(line)# valid IP addressesif result:va

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。