boost regex and match_partial
I'm afraid I haven't got the hang of match_partial. Below is a little code with two functions that I think should produce identical results, but don't. In strip_commas_try1 I use: boost::regex const front("^( *[[],*)(.*)$"); regex_match(it, end, what, front); In strip_commas_try2 I use: boost::regex const front("^( *[[],*)"); regex_match(it, end, what, front, boost::match_partial); Why does this second case not result in what[0].matched ? Many thanks, Angus (This is with BOOST_VERSION 103002 and g++ (GCC) 3.2 20020903 (Red Hat Linux 8.0 3.2-7)). #include<iostream> #include<string> #include <boost/regex.hpp> using std::string; string const strip_commas_try1(string const & input) { string::const_iterator it = input.begin(); string::const_iterator end = input.end(); // Strip any commas that follow immediately after the '['. // "[,,,,foo..." -> "foo..." boost::regex const front("^( *[[],*)(.*)$"); boost::smatch what; regex_match(it, end, what, front); if (!what[0].matched) { std::cout << "Try 1 failed" << std::endl; return string(); } it = what[1].second; string const result = string(it, end); std::cout << "Try 1: " << result << std::endl; return result; } string const strip_commas_try2(string const & input) { string::const_iterator it = input.begin(); string::const_iterator end = input.end(); // Strip any commas that follow immediately after the '['. // "[,,,,foo..." -> "foo..." boost::regex const front("^( *[[],*)"); boost::smatch what; regex_match(it, end, what, front, boost::match_partial); if (!what[0].matched) { std::cout << "Try 2 failed" << '\n'; return string(); } it = what[1].second; string const result = string(it, end); std::cout << "Try 1: " << result << std::endl; return result; } int main() { string const input = "[,,,foo,,,,bar,,,gaz,,]"; std::cout << "Input: " << input << std::endl; strip_commas_try1(input); strip_commas_try2(input); return 0; }
participants (1)
-
Angus Leeming