Possible Duplicate:
No matches with c++11 regex
I was using boost::regex
for some stuff before and for some new stuff I wanted to use std::regex
until I noticed the following inconsistency - so question is which one is correct?
#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.*.*.*.*");
}
For me (gcc 4.7.2, -std=c++11, boost: 1.51), I see the following:
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
If I change the pattern to a greedy pattern (.*
), then I see:
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
Which one to believe? I would guess that boost
is correct here?