2015-11-23 70 views
-2

在我的情況下,我想找到(使用Python正則表達式)所有行而不是包含單詞「錯誤」或「空」。在代碼:如何找到不包含特定單詞的所有行?

varformals:PAR_OPEN PAR_CLOSE {}
| PAR_OPEN varform PAR_CLOSE {}
/* error /|錯誤PAR_CLOSE {}
/
錯誤/|錯誤變量PAR_CLOSE {}
/
錯誤*/| PAR_OPEN錯誤PAR_CLOSE {}
;

+0

它必須與'正則表達式'? –

+0

正則表達式的使用是有道理的,我敢肯定,這是一個重複,如果你認爲情況並非如此,就進行編輯。 –

+1

其實「Python正則表達式」與正則表達式不同。 (pythex模擬器[pythex.org]) 另外我更喜歡一個「蟒蛇正則表達式」,但如果你有一個很好的選擇,請發佈。 :D – HackSlash

回答

1

Regex101

​​

Regular expression visualization

Debuggex Demo

描述

1st Capturing group (^((?!error|empty).)*$) 
    ^assert position at start of a line 
2nd Capturing group ((?!error|empty).)* 
    Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy] 
    Note: A repeated capturing group will only capture the last iteration. 
    Put a capturing group around the repeated group to capture all 
    iterations or use a non-capturing group instead if you're not interested 
    in the data 
    (?!error|empty) Negative Lookahead - Assert that it is impossible to match the regex below 
     1st Alternative: error 
      error matches the characters error literally (case sensitive) 
     2nd Alternative: empty 
      empty matches the characters empty literally (case sensitive) 
    . matches any character (except newline) 
$ assert position at end of a line 
g modifier: global. All matches (don't return on first match) 
m modifier: multi-line. Causes^and $ to match the begin/end of each line (not only begin/end of string) 
+1

只有'空'這個詞在OP的問題中缺失:'(?!error | empty)' – agold

+0

@agold謝謝你指出,更新的答案 – abc123

+0

首先我也認爲這是答案,但是這個正規的正則表達式聲明顯然不適用於Python正則表達式。 (測試請參閱:pythex.org) – HackSlash

相關問題