2017-08-09 27 views
0

我當前的代碼是:如何匹配Poco :: RegularExpression C++中的「 n」?

#include <iostream> 
#include <Poco/Foundation.h> 
#include <Poco/RegularExpression.h> 

int main() 
{ 
    Poco::RegularExpression regex("[A-Z]+\s+[A-Z]+"); 
    Poco::RegularExpression::MatchVec mvec; 
    constad std::string astring = "ABC\nDEFG"; 

    int matches = regex.match(astring,0,mvec); 

    std::cout << "Hello World\n"; 

    return 0; 
} 

我想匹配就可以了,一個空格,多個空格或新行(所以爲什麼我使用空白字符串中的「\ n」的位置元字符)。

返回的匹配數爲零。有沒有我需要設置的標誌或什麼?

+0

加倍反斜槓,因爲'\ s'是無法識別的轉義序列在字符串文字中。 – nhahtdh

+0

對於正則表達式,不是_line-end_'$'嗎? – user0042

回答

0

的問題是在你的正則表達式的景觀序列。

在想一個反斜槓(\)添加到字符串astring,使用令牌\s這種情況下,但在C/C++或Java它必須被當作使用雙\\。因此,要解決你的問題,你必須添加一個反斜槓:

Poco::RegularExpression regex("[A-Z]+\\s+[A-Z]+"); 

在這裏你可以找到參考:

http://en.cppreference.com/w/cpp/language/escape

0

這應該工作

Poco::RegularExpression s ("\\s"); // White char 
Poco::RegularExpression n ("\\n"); // New line 
Poco::RegularExpression r ("\\r"); // Carrige return 
Poco::RegularExpression t ("\\t"); // Tabulator