Matching multiple regex groups and removing them

被刻印的时光 ゝ 提交于 2019-12-05 19:35:34
import re

x = '''LINE: 1
TOKENKIND: somedata
TOKENKIND: somedata
LINE: 2
TOKENKIND: somedata
LINE: 3'''

junkre = re.compile(r'(\s*LINE:\s*\d*\s*)|(\s*TOKENKIND:)', re.DOTALL)

print junkre.sub('', x)

no need to use regex in Python. Its Python after all, not Perl. Think simple and use its string manipulation capabilities

f=open("file")
for line in f:
    if line.startswith("LINE:"): continue
    if "TOKENKIND" in line:
        print line.split(" ",1)[-1].strip()
f.close()

How about replacing (^LINE: \d+$)|(^\w+:) with an empty string ""?

Use \n instead of ^ and $ to remove unwanted empty lines also.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!