std::regex_constants:: error_type
|
定义于头文件
<regex>
|
||
|
using
error_type
=
/* 由实现定义 */
;
|
(1) | (C++11 起) |
|
constexpr
error_type error_collate
=
/* 未指定 */
;
constexpr
error_type error_ctype
=
/* 未指定 */
;
|
(2) |
(C++11 起)
(C++17 起为内联) |
error_type
是一种描述正则表达式解析过程中可能发生的错误的类型。
目录 |
常量
| 名称 | 说明 |
error_collate
|
表达式包含无效的排序规则元素名称 |
error_ctype
|
表达式包含无效的字符类名称 |
error_escape
|
表达式包含无效的转义字符或尾随转义符 |
error_backref
|
表达式包含无效的反向引用 |
error_brack
|
表达式包含不匹配的方括号( '[' 和 ']' ) |
error_paren
|
表达式包含不匹配的圆括号( '(' 和 ')' ) |
error_brace
|
表达式包含不匹配的花括号( '{' 和 '}' ) |
error_badbrace
|
表达式在 { } 表达式中包含无效范围 |
error_range
|
表达式包含无效的字符范围(例如 [b-a]) |
error_space
|
内存不足,无法将表达式转换为有限状态机 |
error_badrepeat
|
'*' 、 '?' 、 '+' 或 '{' 前面没有有效的正则表达式 |
error_complexity
|
尝试匹配的复杂度超过了预设级别 |
error_stack
|
内存不足,无法执行匹配操作 |
示例
实现正则表达式检查器:
#include <cstddef> #include <iomanip> #include <iostream> #include <regex> #include <sstream> #include <string> void regular_expression_checker(const std::string& text, const std::string& regex, const std::regex::flag_type flags) { std::cout << "Text: " << std::quoted(text) << '\n' << "Regex: " << std::quoted(regex) << '\n'; try { const std::regex re{regex, flags}; const bool matched = std::regex_match(text, re); std::stringstream out; out << (matched ? "MATCH!\n" : "DOES NOT MATCH!\n"); std::smatch m; if (std::regex_search(text, m, re); !m.empty()) { out << "prefix = [" << m.prefix().str().data() << "]\n"; for (std::size_t i{}; i != m.size(); ++i) out << " m[" << i << "] = [" << m[i].str().data() << "]\n"; out << "suffix = [" << m.suffix().str().data() << "]\n"; } std::cout << out.str() << '\n'; } catch (std::regex_error& e) { std::cout << "Error: " << e.what() << ".\n\n"; } } int main() { constexpr std::regex::flag_type your_flags = std::regex::flag_type{0} // 选择支持的语法之一: | std::regex::ECMAScript // | std::regex::basic // | std::regex::extended // | std::regex::awk // | std::regex::grep // | std::regex::egrep // 选择以下任意选项: // | std::regex::icase // | std::regex::nosubs // | std::regex::optimize // | std::regex::collate // | std::regex::multiline ; const std::string your_text = "Hello regular expressions."; const std::string your_regex = R"(([a-zA-Z]+) ([a-z]+) ([a-z]+)\.)"; regular_expression_checker(your_text, your_regex, your_flags); regular_expression_checker("Invalid!", R"(((.)(.))", your_flags); regular_expression_checker("Invalid!", R"([.)", your_flags); regular_expression_checker("Invalid!", R"([.]{})", your_flags); regular_expression_checker("Invalid!", R"([1-0])", your_flags); }
可能的输出:
Text: "Hello regular expressions."
Regex: "([a-zA-Z]+) ([a-z]+) ([a-z]+)\\."
MATCH!
prefix = []
m[0] = [Hello regular expressions.]
m[1] = [Hello]
m[2] = [regular]
m[3] = [expressions]
suffix = []
Text: "Invalid!"
Regex: "((.)(.)"
Error: Mismatched '(' and ')' in regular expression.
Text: "Invalid!"
Regex: "[."
Error: Unexpected character within '[...]' in regular expression.
Text: "Invalid!"
Regex: "[.]{}"
Error: Invalid range in '{}' in regular expression.
Text: "Invalid!"
Regex: "[1-0]"
Error: Invalid range in bracket expression..
缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的C++标准。
| 缺陷报告 | 适用范围 | 发布时行为 | 正确行为 |
|---|---|---|---|
| LWG 2053 | C++11 | 常量被声明为 static | 移除了 static 说明符 |
参见
|
(C++11)
|
报告正则表达式库生成的错误
(类) |