Mismatch between boost :: regex and std :: regex

Possible duplicate:
No matches with C ++ 11 regex

I used boost::regex for some things before and for some new things that I wanted to use std::regex until I noticed the following inconsistency - so the 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 you change the pattern to a greedy pattern ( .* ), 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 

Who to believe? I would suggest that boost here is correct?

+4
source share
1 answer

gcc, of course, does not support the tr1 / C ++ 11 regular expression, but to give a more general answer, the default boost.regex value is perl 5 , according to its documentation, while C ++ defaults to ECMAScript extended several language elements of POSIX BRE.

In particular, boost.regex supports the extension extensions listed here . but you do not use them.

Now I was curious and ran the test through two more compilers:

Exiting clang:

 ~ $ 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 

Exit Visual Studio 2012 (no promotion)

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

Acquainted with the clang divergence, in the second test he compared the pattern [FOO\.\*.*?] With [FOO.*] And left [.*.*.*] Unsurpassed, which quickly reduces to comparing [S*?] By differently from boost / visual studio .. which, I think, is also a mistake.

+6
source

Source: https://habr.com/ru/post/1447909/


All Articles