Regex Search in Python: Exclude port 22 lines with ' line 22 '
<p>My current regex search in python looks for lines with <code>' 22 '</code>, but I would like to exclude lines that have <code>' line 22 '</code>. How could I express this in <code>Regex</code>? Would I be <code>'.*(^line) 22 .*$'</code></p>

<pre><code>import re

sshRegexString='.* 22 .*$'
sshRegexExpression=re.compile(sshRegexString)
</code></pre>

<p>You current requirement to find a line that <em>contains</em> <code> 22 </code> but does not contain <code>line 22 </code> can  be implemented without the help of a regex. </p>

<p>Just check if these texts are <code>in</code> or are <code>not in</code> the string inside list comprehension. Here is a <a href="http://ideone.com/o2jFU7" rel="nofollow">Python demo</a> (I assume you have lines in a list, but it can be adjusted to handling lines read from file one by one):</p>

<pre><code>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!
</code></pre>

python regex