'''
功能:通过正则表达式匹配获取相机编号和相机名称
时间:2016-05-30
'''
#coding=gbk
import codecs
import re
#打开文件
fp = codecs.open(r'E:\script\1.txt','r','utf-8')
#匹配像'262245'的六位相机编号
r1 = '2\d{5}'
#匹配像'(2850-G-53)'或'(7泊机房-56)'样式相机名称
r2 = '(\d{4}-\w-\d{1,2}|\d*[\u4E00-\u9FFF]+-\d{1,2})'
for line in fp.readlines():
ID = re.findall(r1,line)
name = re.findall(r2,line)
print('ID = %s,name = %s' % (ID,name))
fp.close()
'''
源文件内容:
[2016-05-23 15:51:07]:262245(2850-G-53):总空间:3257,已用:1776,剩余:1481,预计剩余存储9天,ok.
[2016-05-23 15:51:07]:262247(2851-G-54):总空间:1629,已用:825,剩余:804,预计剩余存储10天,ok.
[2016-05-23 15:51:07]:262249(七楼机房-55):总空间:1457,已用:979,剩余:478,预计剩余存储6天,ok.
[2016-05-23 15:51:07]:262251(7泊机房-56):总空间:1457,已用:710,剩余:747,ok.
运行结果: ID = ['262245'],name = ['2850-G-53'] ID = ['262247'],name = ['2851-G-54'] ID = ['262249'],name = ['七楼机房-55'] ID = ['262251'],name = ['7泊机房-56']
原文链接:https://www.f2er.com/regex/359208.html