std::regex_constants::error_type
來自 cppreference.com
在標頭檔案 <regex> 中定義 |
||
using error_type = /* implementation-defined */; |
(1) | (C++11 起) |
constexpr error_type error_collate = /* unspecified */; constexpr error_type error_ctype = /* unspecified */; |
(2) | (C++11 起) (C++17 起為 inline) |
1)
error_type
是一種描述在正則表示式解析過程中可能發生的錯誤的型別。目錄 |
[edit] 常量
名稱 | 解釋 |
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
|
沒有足夠的記憶體來執行匹配 |
[edit] 示例
實現正則表示式檢查器
執行此程式碼
#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} // Choose one of the supported grammars: | std::regex::ECMAScript // | std::regex::basic // | std::regex::extended // | std::regex::awk // | std::regex::grep // | std::regex::egrep // Choose any of the next options: // | 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..
[edit] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 2053 | C++11 | 常量被宣告為 static | 移除了 static 說明符 |
[edit] 參見
(C++11) |
報告正則表示式庫生成的錯誤 (類) |