#include<iostream>
#include <regex>
void DescriptionRegexError(const std::regex_error& e);
int main(int argc, char **argv)
{
(void)argc; (void)argv;
try {
std::regex nameRegex("^[a-zA-Z][[:word:]]+");
try {
const auto r = std::regex_match("someDefaultName", nameRegex);
}
catch (const std::regex_error& e) {
std::cout<<"invalid: const auto r = std::regex_match(\"someDefaultName\", nameRegex);\n";
DescriptionRegexError(e);
}
}
catch (const std::regex_error& e) {
std::cout<<"invalid: std::regex nameRegex(\"^[a-zA-Z][[:word:]]+\");\n";
DescriptionRegexError(e);
}
std::cout<<"-----------------------\n";
}
void DescriptionRegexError(const std::regex_error& e)
{
//std::cout << "regex_error caught: " << e.what() << '\n';
//std::cout<< e.code() << '\n';
if(e.code()==std::regex_constants::error_collate)
std::cout << "the expression contains an invalid collating element name\n";
else if(e.code()==std::regex_constants::error_ctype)
std::cout << "the expression contains an invalid character class name\n";
else if(e.code()==std::regex_constants::error_escape)
std::cout << "the expression contains an invalid escaped character or a trailing escape\n";
else if(e.code()==std::regex_constants::error_backref)
std::cout << "the expression contains an invalid back reference\n";
else if(e.code()==std::regex_constants::error_brack)
std::cout << "the expression contains mismatched square brackets ('[' and ']')\n";
else if(e.code()==std::regex_constants::error_paren)
std::cout << "the expression contains mismatched parentheses ('(' and ')')\n";
else if(e.code()==std::regex_constants::error_brace)
std::cout << "the expression contains mismatched curly braces ('{' and '}')\n";
else if(e.code()==std::regex_constants::error_badbrace)
std::cout << "the expression contains an invalid range in a {} expression\n";
else if(e.code()==std::regex_constants::error_space)
std::cout << "there was not enough memory to convert the expression into a finite state machine\n";
else if(e.code()==std::regex_constants::error_badrepeat)
std::cout << "one of *?+{ was not preceded by a valid regular expression\n";
else if(e.code()==std::regex_constants::error_complexity)
std::cout << "the complexity of an attempted match exceeded a predefined level\n";
else if(e.code()==std::regex_constants::error_stack)
std::cout << "there was not enough memory to perform a match\n";
#ifdef _MSC_VER //microsoft specific
else if(e.code()==std::regex_constants::error_parse)
std::cout << "error_parse\n";
else if(e.code()==std::regex_constants::error_syntax)
std::cout << "error_syntax\n";
#endif
}