2009-10-08 67 views
2

有沒有人有任何想法爲什麼下面的代碼會輸出「不匹配」?Boost.Regex oddity

boost::regex r(".*\\."); 
    std::string s("app.test"); 
    if (boost::regex_match(s, r)) 
    std::cout << "match" << std::endl; 
    else 
    std::cout << "no match" << std::endl; 

回答

4

我相信對整個字符串regex_match()匹配。改爲嘗試regex_search()

它將與下面的正則表達式工作過:

boost::regex r(".*\\..*"); 

regex_match()功能。但是,regex_search()是您可能要查找的內容。

+0

+1 - 「確定正則表達式e和所有字符序列之間是否存在精確匹配」。見http://www.boost.org/doc/libs/1_40_0/libs/regex/doc/html/boost_regex/ref/regex_match.html – 2009-10-08 15:04:13

+0

雖然我認爲他真的想'regex_search()'。 – 2009-10-08 15:05:17

+0

確實,就是這樣。 我現在覺得很愚蠢。 不過,謝謝你的回答 – ldx 2009-10-08 15:10:44