regex_error in C++

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

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

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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";
        }
    }
}

chevron_right


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



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.