C++ 中的 regex_error
regex 库包含与正则表达式相关的多种方法和功能。接下来,我们介绍一些 regex_errors。这些也是 regex 库中存在的。在执行一些正则表达式时,我们可能会遇到一些错误。这些错误如下。
| 标志 | 错误 |
|---|---|
| error_collate | Regex 中名称校对无效。 |
| error_ctype | Regex 中包含无效的字符类名称。 |
| error_stack | 没有足够的内存来确定是否可以匹配 regex。 |
| error_space | 内存不足时,转换为有限状态机 |
| error_badrepeat | 字符串包含未在前面加上有效正则表达式的重复指定符 ( *?+{)。 |
| error_complexity | 尝试与 regex 进行匹配的复杂度超过了预设级别 |
| error_range | 包含无效字符范围。 |
| error_badbrace | Regex 中包含不匹配的花括号 { 和 }。 |
| error_brace | Regex 中包含 { 和 } 之间无效的范围。 |
| error_paren | Regex 中包含不匹配的小括号 ( 和 )。 |
| error_brack | Regex 中包含不匹配的中括号 ([ 和 ])。 |
| error_backref | Regex 其中除外无效的反向引用。 |
| error_escape | Regex 中不允许任何无效的转义字符或尾随转义符。 |
示例
#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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP