1
我在Python中的當前正則表達式搜索查找與' 22 '
行,但我想排除有' line 22 '
行。我怎麼能在Regex
表達這個?我會'.*(^line) 22 .*$'
在Python中的正則表達式搜索:排除22行'22'
import re
sshRegexString='.* 22 .*$'
sshRegexExpression=re.compile(sshRegexString)
我在Python中的當前正則表達式搜索查找與' 22 '
行,但我想排除有' line 22 '
行。我怎麼能在Regex
表達這個?我會'.*(^line) 22 .*$'
在Python中的正則表達式搜索:排除22行'22'
import re
sshRegexString='.* 22 .*$'
sshRegexExpression=re.compile(sshRegexString)
您當前需求找到包含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!
你的最終代碼是什麼?什麼是輸出?問題是什麼 ? – aluriak
'。*(?<!line)22。* $' – revo
@revo那個正則表達式起作用了!謝謝 – pHorseSpec