std::regex_constants::error_type
来自cppreference.com
<tbody>
</tbody>
| 在标头 <regex> 定义
|
||
using error_type = /* 由实现定义 */; |
(1) | (C++11 起) |
constexpr error_type error_collate = /* 未指定 */; constexpr error_type error_ctype = /* 未指定 */; constexpr error_type error_escape = /* 未指定 */; constexpr error_type error_backref = /* 未指定 */; constexpr error_type error_brack = /* 未指定 */; constexpr error_type error_paren = /* 未指定 */; constexpr error_type error_brace = /* 未指定 */; constexpr error_type error_badbrace = /* 未指定 */; constexpr error_type error_range = /* 未指定 */; constexpr error_type error_space = /* 未指定 */; constexpr error_type error_badrepeat = /* 未指定 */; constexpr error_type error_complexity = /* 未指定 */; constexpr error_type error_stack = /* 未指定 */; |
(2) | (C++11 起) (C++17 起 inline) |
1)
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 << "文本:" << std::quoted(text) << '\n'
<< "正则:" << std::quoted(regex) << '\n';
try
{
const std::regex re{regex, flags};
const bool matched = std::regex_match(text, re);
std::stringstream out;
out << (matched ? "匹配!\n" : "不匹配!\n");
std::smatch m;
if (std::regex_search(text, m, re); !m.empty())
{
out << "前缀 = [" << m.prefix().str().data() << "]\n";
for (std::size_t i{}; i != m.size(); ++i)
out << " m[" << i << "] = [" << m[i].str().data() << "]\n";
out << "后缀 = [" << m.suffix().str().data() << "]\n";
}
std::cout << out.str() << '\n';
}
catch (std::regex_error& e)
{
std::cout << "错误:" << 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);
}
可能的输出:
文本:"Hello regular expressions."
正则:"([a-zA-Z]+) ([a-z]+) ([a-z]+)\\."
MATCH!
前缀 = []
m[0] = [Hello regular expressions.]
m[1] = [Hello]
m[2] = [regular]
m[3] = [expressions]
后缀 = []
文本:"Invalid!"
正则:"((.)(.)"
错误:Mismatched '(' and ')' in regular expression.
文本:"Invalid!"
正则:"[."
错误:Unexpected character within '[...]' in regular expression.
文本:"Invalid!"
正则:"[.]{}"
错误:Invalid range in '{}' in regular expression.
文本:"Invalid!"
正则:"[1-0]"
错误:Invalid range in bracket expression..
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| LWG 2053 | C++11 | 常量被声明为 static
|
移除 static 说明符
|
参阅
(C++11) |
报告正则表达式库生成的错误 (类) |