2016-09-23 83 views
1

我在Python中的当前正则表达式搜索查找与' 22 '行,但我想排除有' line 22 '行。我怎么能在Regex表达这个?我会'.*(^line) 22 .*$'在Python中的正则表达式搜索:排除22行'22'

import re 

sshRegexString='.* 22 .*$' 
sshRegexExpression=re.compile(sshRegexString) 
+0

你的最终代码是什么?什么是输出?问题是什么 ? – aluriak

+3

'。*(?<!line)22。* $' – revo

+0

@revo那个正则表达式起作用了!谢谢 – pHorseSpec

回答

0

您当前需求找到包含22但不包含line 22可以在没有正则表达式的帮助下实现的线。

只要检查这些文字是in还是not in列表理解中的字符串。这里是一个Python demo(我假设你有一个列表中的行,但它可以调整处理从文件中逐行读取的行):

lines = ['Some text 1 line 22 here', 'Some text 2 Text2 22 here', 'Some text 3 Text3 22 here'] 
good = [s for s in lines if ' 22 ' in s and 'line 22 ' not in s] 
print(good) # the first lines[0] is not printed!