名前空間
変種
操作

std::regex_constants::error_type

From cppreference.com
< cpp‎ | regex
 
 
 
正規表現ライブラリ
クラス
(C++11)
アルゴリズム
イテレータ
例外
Traits
定数
error_type
(C++11)
正規表現文法
 
ヘッダ <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以降インライン化)
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 << "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..

[編集] 不具合報告

以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。

DR 適用対象 公開された動作 正しい動作
LWG 2053 C++11 定数が static で宣言されていました static 指定子が削除されました

[編集] 関連項目

正規表現ライブラリによって生成されたエラーを報告する
(クラス) [編集]
English 日本語 中文(简体) 中文(繁體)