regex_error in C++


The regex library has different methods and features related to regular expressions. Here we will see some regex_errors. These are also present at regex library. During executing some regular expressions, we get some errors. That errors are mentioned here.

FlagsErrors
error_collateIn the Regex, the names having invalid collation.
error_ctypeIn the Regex, there is an invalid character class name.
error_stackNot enough memory to determine regex can be matched or not.
error_spaceConvert into Finite State Machine, when memory is insufficient
error_badrepeatThe string has repeat specifier ( *?+{) that was not preceded by a valid regular expression.
error_complexityThe complexity of an attempted match against a regex exceeded a pre-set level
error_rangeContaining invalid character range.
error_badbraceThe regex contains mismatched braces { and }.
error_braceThe regex contains invalid range between braces { and }.
error_parenThe regex contains mismatched parentheses ( and ).
error_brackThe regex contains mismatched brackets ([ and ]).
error_backrefThe regex excepts invalid back reference.
error_escapeThe regex does not allows any invalid escaped character, or a trailing escape.

Example

#include <iostream>
#include <regex>
int main() {
   try {
      std::regex re("[A-Z][0"); //an error is present
   } catch (const std::regex_error& err) {
      std::cout << "There is an error. The error is: " << err.what() << '\n';
      if (err.code() == std::regex_constants::error_brack) {
         std::cout << "This is the code of error_brack\n";
      }
   }
}

Output

There is an error. The error is: Unexpected character in bracket expression.
This is the code of error_brack

Updated on: 30-Jul-2019

165 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements