形成正则表达式

时间:2019-04-06 17:03:05

标签: regex python-3.x

我正在编写代码,将特定部分从文本文件复制到另一个文本文件。我想知道我应该形成什么正则表达式,以便在每个RelationRecord中将第一个花括号内的内容复制到文本文件中。有人可以帮我吗?

这是我输入文件的内容:

[RelationRecord(items=frozenset({'   android.software.live_wallpaper\n', '   location.gps\n'}), support=0.010217983651226158, ordered_statistics=[OrderedStatistic(items_base=frozenset({'   android.software.live_wallpaper\n'}), items_add=frozenset({'   location.gps\n'}), confidence=0.6818181818181819, lift=2.194976076555024), OrderedStatistic(items_base=frozenset({'   location.gps\n'}), items_add=frozenset({'   android.software.live_wallpaper\n'}), confidence=0.03289473684210527, lift=2.1949760765550246)]), RelationRecord(items=frozenset({'   location\n', '   location.gps\n'}), support=0.3106267029972752, ordered_statistics=[OrderedStatistic(items_base=frozenset({'   location\n'}), items_add=frozenset({'   location.gps\n'}), confidence=0.7808219178082192, lift=2.5136986301369864), OrderedStatistic(items_base=frozenset({'   location.gps\n'}), items_add=frozenset({'   location\n'}), confidence=1.0, lift=2.5136986301369864)]),

代码:

import re
import sys
regex = r"\RelationRecord(items=frozenset({(.*?)\}"
myfile = open("CEHRelationRecord.txt", "rt") 
test_str = myfile.read()                  
myfile.close()  
matches = re.finditer(regex, test_str, re.MULTILINE | re.DOTALL)

我希望将以下内容作为给定输入的输出:

line1:  android.software.live_wallpaper, location.gps
line2:  location, location.gps

0 个答案:

没有答案