<分区>
Possible Duplicate:
No matches with c++11 regex
我之前使用 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,boost:1.51),我看到了以下内容:
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
在这里是正确的吗?