Open In App

regex_error in C++

Improve
Improve
Like Article
Like
Save
Share
Report

regex_error is present inside the Header “regex” and inside the Class regex_error;. It helps us to know about the errors which are thrown during the program execution, it defines the type of the object of exception in regular expressions library, Also describes an error in the construction or use of the basic_regex object. It plays an important role to construct an object that use to holds the value error and this is inherited from std:: exception

There are thirteen types of errors which are listed below:

FLAGS ERRORS
error_collate This expressions have element names having invalid collation.
error_ctype This expressions have an invalid character class name.
error_stack If there is not enough memory to determine whether the regular expressions can match a give characters sequence.
error_space This occurs to convert into finite state machine when memory is insufficient.
error_badrepeat This contains a repeat specifier ( *?+{) that was not preceded by a valid regular expression.
error_complexity The complexity of an attempted match against a regular expression exceeded a pre-set level
error_range when contains an invalid character range.
error_badbrace The expression contains invalid range between braces { and }.
error_brace The expression contains mismatched braces { and }.
error_paren The expression contains mismatched parentheses ( and ).
error_brack The expression contains mismatched brackets ([ and ]).
error_backref The expression excepts invalid back reference.
error_escape The expression does not allows any invalid escaped character, or a trailing escape.
error_escape The expression does not allows any invalid escaped character, or a trailing escape.

Below is a simple program which demonstrates regex-error.

Program 1:




// Program to demonstrate the error
int main()
{
#include <iostream>
#include <regex>
  
    int main()
    {
        try {
            std::regex re("[1-9][0");
        }
  
        catch (const std::regex_error& err) {
            std::cout << "There was a regex_error caughted: " 
            << err.what() << '\n';
            if (err.code() == std::regex_constants::error_brack) {
                std::cout << "The code gives an error_brack\n";
            }
        }
    }
    return 0;
}


Output:

regex_error caught: Unexpected character in bracket expression.
        The code gives an error_brack

Program 2:




// Program to demonstrate no error
#include <iostream>
#include <regex>
  
int main()
{
    try {
        std::regex re("[A-Z][bcd] ");
    }
  
    catch (const std::regex_error& er) {
        std::cout << "regex_error caught: "
        << er.what() << '\n';
        if (er.code() == std::regex_constants::error_brack) {
            std::cout << "The code was this is the error error_brack\n";
        }
    }
}


Note: There is no output as no error is there.



Last Updated : 04 Sep, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments