我有下面的代码,我想要做什么,从该命令的结果检索包名称:的Python:正则表达式
命令:
dpkg --get-selections | grep amule
字符串分析:
string = 'amule\t\t\t\t\t\tinstall\namule-common\t\t\t\t\tinstall\namule-utils\t\t\t\t\tinstall\n'
代码:
pattern = re.compile(r"[a-z](.*)\w*(?=([\\\t]*install))")
matches = re.finditer(pattern, result[0])
for match in matches:
plist.append(match.group().strip())
结果:
plist = ['amule', 'amule-common', 'amule-utils']
但我想来优化代码,不使用条带的功能和仅使用正则表达式获得相同的结果。到目前为止,即使在'install'字符串之前使用'+','*'或{n},我也无法摆脱所有'\ t'。任何想法 ?
谢谢
2个信息(网站和正则表达式)在1个答案:谢谢! –
不客气! – brandonscript
对不起再次打扰,但实际上代码确实可以在网页上使用,但不是在第一篇文章中编码的字符串。我试图用他们的界面修改它,但没有任何成功。我到目前为止的Python代码是:p = re.compile(r'([\ w \ - ] +)[\\ t] * install [\\ n] *',re.MULTILINE) str =“amule \ t \ t \ t \ t \ tinstall \ namule-common \ t \ t \ t \ t \ tinstall \ namule-utils \ t \ t \ t \ t \ tinstall \ n \ n“ p.match (str)在网站上工作,但不在我的shell中。网站:http://regex101.com/r/wA4kZ9 –