1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
#!/usr/bin/env python # coding=utf-8 import re datas = [] file = open("/tmp/test.txt") while 1: line = file.readline() if not line: break datas.append(line.replace("\n","")) file.close() print len(datas) #print datas for i in range(1,len(datas),2): first_part = datas[i].replace('redhat-abc-','').replace('CDN:','') seconf_part = datas[i-1] pattern = re.compile(r'-(v|[0-9]).*$') mach_for2 = pattern.search(seconf_part) #print mach_for2 if mach_for2: #print 'group:',mach_for2.group() final = first_part+":"+mach_for2.group().lstrip()[1:] print str(final).replace(" ","") else: print 'this line have some issue:',first_part print 'second:',seconf_part |
在写这个程序的时候,正则表达式卡我半天 ….
如果你使用 match, 则正则表达式中一定要包含 ^.* ,表示从头开始匹配,这与我的目标不符合,所以改用seach ,
Latest posts by Zhiming Zhang (see all)
- aws eks node 自动化扩展工具 Karpenter - 8月 10, 2022
- ReplicationController and ReplicaSet in Kubernetes - 12月 20, 2021
- public key fingerprint - 5月 27, 2021
kuyus 2016/10/19 14:41
路过~混个脸熟。