[regex] stuck with expression syntax / exception
Hi, I am pretty much a complete novice to the boost::regex library, and stuck with the following snippet: std::string String = " (of 1000 elements; 67.10%)"; try { std::cout << boost::regex_match(String, boost::regex("^ (of \\d+ elements; .+%)$")) << std::endl; std::cout << boost::regex_match(String, boost::regex(" (of \\d+ elements;.+")) << std::endl; } catch(boost::regex_error const &) { std::cout << "caught boost::regex_error!"; } Two questions: First, why does the first not match? Second, why does the second throw? The idea of the regex is that the first number is an integer, and for the percentage number the formatting is unspecified but must terminate with '%'; otherwise all characters must match exactly. thanks, Thomas
On 14/03/2015 16:31, Thomas wrote:
Hi,
I am pretty much a complete novice to the boost::regex library, and stuck with the following snippet:
std::string String = " (of 1000 elements; 67.10%)"; try { std::cout << boost::regex_match(String, boost::regex("^ (of \\d+ elements; .+%)$")) << std::endl; std::cout << boost::regex_match(String, boost::regex(" (of \\d+ elements;.+")) << std::endl; } catch(boost::regex_error const &) { std::cout << "caught boost::regex_error!"; }
Two questions: First, why does the first not match? The "(" characters are grouping operators, use "\\(" for a literal (.
Second, why does the second throw?
Well if you'd printed out the message contained in the exception it would have told ;) Same reason as above: you have a grouping ( start but no matching ). John.
The idea of the regex is that the first number is an integer, and for the percentage number the formatting is unspecified but must terminate with '%'; otherwise all characters must match exactly.
thanks, Thomas
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On 14/03/2015 18:02, John Maddock wrote:
Well if you'd printed out the message contained in the exception it would have told ;)
Apparently in my regular environment I am not used to getting informative exception messages, so I forget the possibility it may so be in boost ;) Thanks, it works fine now! Thomas
participants (2)
-
John Maddock
-
Thomas