2013-11-28 261 views
1

我有下面的代码,我想要做什么,从该命令的结果检索包名称:的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'。任何想法 ?

谢谢

回答

0

好了,在您的帮助(反斜杠是问题),这是我能想出

pattern = re.compile(r'([\w\-]+)(?=(\s*install\s*))', re.MULTILINE) 
matches = re.finditer(pattern, string_to_analize) 

for match in matches: 
    print match.group() 

它确实需要什么。

非常感谢您的帮助! ;)

PS:只是一个很奇怪的事情:该正则表达式不起作用的网站,你明白为什么? http://regex101.com/r/iM2gJ1

1

您应该能够通过使用re.M标志(多)要做到这一点很容易。

"([\w\-]+)\s*install", re.M

像这样:

match = re.search(r"([\w\-]+)\s*install", re.M) 
if match: 
    plist = match 

看到这里工作的例子:http://regex101.com/r/jE0dL8

+0

2个信息(网站和正则表达式)在1个答案:谢谢! –

+0

不客气! – brandonscript

+0

对不起再次打扰,但实际上代码确实可以在网页上使用,但不是在第一篇文章中编码的字符串。我试图用他们的界面修改它,但没有任何成功。我到目前为止的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 –