Python正则表达式IP no resu

2024-04-26 02:22:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了一些代码从批量文本中提取电子邮件和IP地址。但是,代码只提取电子邮件地址。(我想让大家理解的原文是一个典型的日志文件)。我不知道为什么生成的文件没有返回IP地址。你知道吗

import os
import re

# 1
filename = 'errors.txt'
newfilename = 'emaillist-rev.txt'

# 2
if os.path.exists(filename):
    data = open(filename,'r')
    bulkemails = data.read()


else:
    print "File not found."
    raise SystemExit

# 3
r = re.compile(r'[\w\.-]+@[\w\.-]+')
results = r.findall(bulkemails)    

emails = ""   
for x in results:
    emails += str(x)+"\n"   


# 4
ip = re.compile('^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$')
result = ip.findall(bulkemails)

ip =""
for y in result:
    ip += str(y)+"\n"

# 5
def writefile():
    f = open(newfilename, 'w')
    f.write(emails + ip)
    f.close()
    print "File written."

# 6
def overwrite_ok():
    response = raw_input("Are you sure you want to overwrite "+str(newfilename)+"? Yes or No\n")
    if response == "Yes":
        writefile()
    elif response == "No":
        print "Aborted."
    else:
        print "Please enter Yes or No."
        overwrite_ok()

# 7
if os.path.exists(newfilename):
    overwrite_ok()      
else: 
    writefile()

Tags: ipreifosresponseokfilenameelse
1条回答
网友
1楼 · 发布于 2024-04-26 02:22:21

在声明ip正则表达式时,用单词边界替换锚点,注意需要使用原始字符串文本。你知道吗

ip = re.compile(r'\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b')

相关问题 更多 >