C++ 中的 regex_error


regex 库包含与正则表达式相关的多种方法和功能。接下来,我们介绍一些 regex_errors。这些也是 regex 库中存在的。在执行一些正则表达式时,我们可能会遇到一些错误。这些错误如下。

标志错误
error_collateRegex 中名称校对无效。
error_ctypeRegex 中包含无效的字符类名称。
error_stack没有足够的内存来确定是否可以匹配 regex。
error_space内存不足时,转换为有限状态机
error_badrepeat字符串包含未在前面加上有效正则表达式的重复指定符 ( *?+{)。
error_complexity尝试与 regex 进行匹配的复杂度超过了预设级别
error_range包含无效字符范围。
error_badbraceRegex 中包含不匹配的花括号 { 和 }。
error_braceRegex 中包含 { 和 } 之间无效的范围。
error_parenRegex 中包含不匹配的小括号 ( 和 )。
error_brackRegex 中包含不匹配的中括号 ([ 和 ])。
error_backrefRegex 其中除外无效的反向引用。
error_escapeRegex 中不允许任何无效的转义字符或尾随转义符。

示例

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

输出

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

更新日期: 2019-07-30

181 次浏览

开启您的职业生涯

完成课程即可获得认证

开始入门
广告
© . All rights reserved.