2015-06-26 48 views
-4

我需要一個找到除了this String 例如數據事情是:正則表達式來找到任何東西,除了「這個字符串」

this is one line with this string only this should not match 
this is another line but has no string in 
this String is another 
and then this line should match also 

我想找到並突出顯示整行,太行 this is one line with this string是隻有一個將被選中。

我試過^(?!(this String)$)但是這找到零長度匹配,所以沒有太大的幫助,我試着在各個地方添加.*,但不明白該怎麼做。

+2

什麼工具您使用? – nhahtdh

+0

您是否試過'^(?:(?!\ b(?:red | blue | green)\ b)。)* $'?順便說一句,爲什麼',然後這條線不應該匹配'不應該匹配?它裏面沒有'this string'。 –

+2

「紅色,藍色」如何進入畫面? – vks

回答

0

機會是,你不需要正則表達式完成這個任務的。看看這兩段代碼:

std::regex pattern("^((?!this string).)*$"); 
while (std::getline(infile, chkme)) { 
    if (std::regex_match(chkme,pattern)) { 
     std::cout << "Not found! " << chkme; 
}} 

std::string pattern = "this string"; 
while (std::getline(infile, chkme)) { 
    if (chkme.find(pattern) == string::npos) { 
     std::cout << "Not found! " << chkme; 
}} 
+0

對不起,不明白他們應該做什麼。 – f1wade

+0

秒更容易閱讀。對於這種簡單的情況,使用正則表達式是過度的。 – ZuOverture

相關問題