📜  C++中的regex_error

📅  最后修改于: 2021-05-30 09:47:36             🧑  作者: Mango

regex_error存在于标题“ regex”内部和类regex_error中; 。它有助于我们了解程序执行过程中引发的错误,它在正则表达式库中定义异常对象的类型,还描述了basic_regex对象的构造或使用中的错误。构造用于保留值错误的对象起着重要作用,该对象继承自std ::异常

下面列出了十三种错误:

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.

下面是一个演示正则表达式错误的简单程序。

程序1:

// Program to demonstrate the error
int main()
{
#include 
#include 
  
    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;
}

输出:

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

程式2:

// Program to demonstrate no error
#include 
#include 
  
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";
        }
    }
}

注意:因为没有错误,所以没有输出。

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”