37,720
社区成员
发帖
与我相关
我的任务
分享
import re
Str = r'''[14/Jul/2014:16:39:22 CST] [4019943168] 10.6.99.163 test1 "CONNECT" STARTED 0 0 0 (10.6.99.163:63548 -> 10.203.19.28:8080)'''
p = re.compile(r"\d+/\w+/\d+:\d+:\d+:\d+")
print p.findall(Str)
p = re.compile(r'\s(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s')
print p.findall(Str)
p = re.compile(r"->\s(.*:\d+)")
print p.findall(Str)
import re
with open(r'C:\Users\admin-ZH\Desktop\111.txt', 'r') as fr:
fw = open(r'C:\Users\admin-ZH\Desktop\222.txt', 'w+')
for text in fr:
times = re.findall(r'(?<=\[).*?(?= CST\])', text)[0]
ips = re.findall(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', text)[0]
test = re.findall(r'(?<={}).*?(?=")'.format(ips), text)[0].strip()
ip_port = re.findall(r'(?<=->).*?(?=\))'.format(ips), text)[0].strip()
infos = ' '.join([times, ips, test, ip_port]) + '\n'
fw.write(infos)
fw.close()
with open(r'C:\Users\admin-ZH\Desktop\222.txt', 'r') as fr:
for text in fr:
print text
同为学生,还是劝楼主不要做伸手党。