2012-11-06 48 views
1

我試圖在Ubuntu中使用C++和PCRE正則表達式。我安裝了幾乎所有相關的軟件(libpcrepp和類似軟件),但我甚至無法匹配最簡單的表達式。我的代碼,簡化:PCRE忽略C++中的匹配

#include <iostream> 
#include <string> 
#include <pcrecpp.h> 

using namespace std; 

int main() { 

    std::string text, a, b; 

    text = "Flowers in the forest are darker than in the prairie"; 

    pcrecpp::RE re("forest"); 

    if(re.PartialMatch(text, &a, &b)) { 
     std::cout << "match: " << a << b << "\n"; 
    } 

} 

沒有錯誤編譯:

g++ t2.cpp -lpcrecpp -o t2 

也沒有結果執行時。任何提示?提前致謝。

回答

1

re.PartialMatch(文本,&一個,& B)

如果有至少兩個捕捉在正則表達式,一個用於每個的返回參數只能返回true。由於正則表達式(「森林」)中沒有捕獲,因此無論模式是否匹配文本,re.PartialMatch都會保證返回false。

+0

我刪除了&B的離開表達: re.PartialMatch(文字,&A) 但它仍然沒有工作。謝謝@rici。 – user1801983

+0

您也沒有一個捕獲組。你讀過關於正則表達式的任何文檔嗎?捕獲組是由'('和')'包圍的正則表達式的一部分,匹配被「捕獲」並返回。既然你沒有這些,你所能做的只是找出匹配是否奏效(布爾返回結果)。如果你使用DoMatch,你也可以得到比賽的長度。 (詳見pcrecpp.h)。 – rici

+0

由()包圍的表達式有訣竅。感謝您的幫助,@rici。 – user1801983