2012-11-23 65 views
4

可能重複:
No matches with c++11 regex不一致升壓之間::正則表達式和std ::正則表達式

我用boost::regex了一些東西前,一些新的東西,我想用std::regex直到我注意到以下不一致 - 所以問題是哪一個是正確的?

#include <iostream> 
#include <regex> 
#include <string> 

#include <boost/regex.hpp> 

void test(std::string prefix, std::string str) 
{ 
    std::string pat = prefix + "\\.\\*.*?"; 

    std::cout << "Input : [" << str << "]" << std::endl; 
    std::cout << "Pattern : [" << pat << "]" << std::endl; 

    { 
    std::regex r(pat); 
    if (std::regex_match(str, r)) 
     std::cout << "std::regex_match: true" << std::endl; 
    else 
     std::cout << "std::regex_match: false" << std::endl; 

    if (std::regex_search(str, r)) 
     std::cout << "std::regex_search: true" << std::endl; 
    else 
     std::cout << "std::regex_search: false" << std::endl; 
    } 

    { 
    boost::regex r(pat); 
    if (boost::regex_match(str, r)) 
     std::cout << "boost::regex_match: true" << std::endl; 
    else 
     std::cout << "boost::regex_match: false" << std::endl; 

    if (boost::regex_search(str, r)) 
     std::cout << "boost::regex_search: true" << std::endl; 
    else 
     std::cout << "boost::regex_search: false" << std::endl; 
    } 
} 

int main(void) 
{ 
    test("FOO", "FOO.*"); 
    test("FOO", "FOO.*.*.*.*"); 
} 

對於我(GCC 4.7.2,-std = C++ 11,升壓:1.51),I看到以下內容:

Input : [FOO.*] 
Pattern : [FOO\.\*.*?] 
std::regex_match: false 
std::regex_search: false 
boost::regex_match: true 
boost::regex_search: true 
Input : [FOO.*.*.*.*] 
Pattern : [FOO\.\*.*?] 
std::regex_match: false 
std::regex_search: false 
boost::regex_match: true 
boost::regex_search: true 

如果更改圖案的貪婪圖案( .*),然後我看到:

Input : [FOO.*] 
Pattern : [FOO\.\*.*] 
std::regex_match: true 
std::regex_search: false 
boost::regex_match: true 
boost::regex_search: true 
Input : [FOO.*.*.*.*] 
Pattern : [FOO\.\*.*] 
std::regex_match: true 
std::regex_search: false 
boost::regex_match: true 
boost::regex_search: true 

哪一個要相信?我猜想boost在這裏是正確的?

+2

Boost很可能是正確的,因爲並非所有標準庫都完全實現了C++ 11。正則表達式庫似乎迄今爲止最容易被忽略,至少在GCC中,而Visual C++中的支持似乎更好。 –

+0

您提供的輸出不能來自您提供的程序。你有'std :: string pat = prefix +「\\。\\ *。*?」;',所以如果'prefix'是'FOO。*',那麼'pat'必須是'FOO。* \ 。\ *。*?',而不是'FOO。*?'。 –

+0

@j_random_hacker,是的 - 對不起,我只是改變了代碼片段 - 如果你運行它,你會得到相同的結果.. – Nim

回答

6

GCC當然不支持TR1/C++ 11的正則表達式,而是提供一個更普遍的答案,boost.regex默認爲的Perl 5,根據其文檔,而C++默認爲的ECMAScript ,由POSIX BRE的幾個取決於區域的元素擴展。

具體而言,boost.regex支持perl擴展listed here.,但您並未使用任何這些擴展。現在

,我得到了好奇,並通過另外兩個編譯器運行測試:

從鐺輸出:從Visual Studio 2012(SANS提高)

Input : [FOO.*] 
Pattern : [FOO\.\*.*?] 
std::regex_match: true 
std::regex_search: true 
Input : [FOO.*.*.*.*] 
Pattern : [FOO\.\*.*?] 
std::regex_match: true 
std::regex_search: true 

尋找

~ $ clang++ -o test test.cc -std=c++11 -I/usr/include/c++/v1 -lc++ -lboost_regex 
~ $ ./test 
Input : [FOO.*] 
Pattern : [FOO\.\*.*?] 
std::regex_match: true 
std::regex_search: true 
boost::regex_match: true 
boost::regex_search: true 
Input : [FOO.*.*.*.*] 
Pattern : [FOO\.\*.*?] 
std::regex_match: false 
std::regex_search: true 
boost::regex_match: true 
boost::regex_search: true 

輸出在第二次測試中,它匹配[FOO\.\*.*?][FOO.*]的模式,並且左側[.*.*.*]不匹配,這快速歸結爲匹配[S*?]不同於增強/視覺工作室..我認爲,這也是一個錯誤。

+0

我相信這絕對是一個錯誤。既然它是經典的擴展​​正則表達式,你可以用'grep -E'輕鬆檢查它,甚至grep也同意boost(grep可以說是最古老的正則表達式引擎之一,並且已經被用戶徹底濫用/測試過)。有趣的是,微軟正確的。 – slebetman

相關問題