Namespaces
Variants

std::regex_constants:: error_type

From cppreference.net
定義済みヘッダー <regex>
using error_type = /* implementation-defined */ ;
(1) (C++11以降)
constexpr error_type error_collate = /* unspecified */ ;

constexpr error_type error_ctype = /* unspecified */ ;
constexpr error_type error_escape = /* unspecified */ ;
constexpr error_type error_backref = /* unspecified */ ;
constexpr error_type error_brack = /* unspecified */ ;
constexpr error_type error_paren = /* unspecified */ ;
constexpr error_type error_brace = /* unspecified */ ;
constexpr error_type error_badbrace = /* unspecified */ ;
constexpr error_type error_range = /* unspecified */ ;
constexpr error_type error_space = /* unspecified */ ;
constexpr error_type error_badrepeat = /* unspecified */ ;
constexpr error_type error_complexity = /* unspecified */ ;

constexpr error_type error_stack = /* unspecified */ ;
(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}
    // サポートされている文法のいずれかを選択:
        | 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++規格に対して遡及的に適用されました。

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

関連項目

正規表現ライブラリによって生成されたエラーを報告する
(クラス)