I'm trying to migrate some regex tools from Qt to the std. In Qt, I can test if a regex is valid before using it with isValid()
In the std's <regex>
, I don't see a way to do this. So for now, I have try/catch
blocks that makes the regex with a user-provided regex and then tries to match it against a 1-char string to quickly trigger the std::regex_error
exception without loading the actual search string(s) so I can quit out early. This is a dirty hack IMO, but I'm not sure if there's a better way to test them efficiently with std::regex
. I'm basically trying to avoid performance hitches from catching and handling exceptions while using automated input with the tool.
try
{
const std::regex regex_exception_trigger(regex_string);
std::smatch stability_match;
const std::string test_string = "0";
if (std::regex_search(test_string.begin(), test_string.end(), stability_match, regex_exception_trigger)) {}
}
catch (std::regex_error &re) { std::cerr << re.what() << std::endl; print_help(); return exit_enum::BAD_REGEX; }