GeezerVison is showing cpprefjp.github.io — std::regex_error - cpprefjp C++日本語リファレンス
最終更新日時:
2026年10月03日 22時31分04秒 (JST)
Akira Takahashi
が更新
履歴
編集
class template
<regex>
std:: regex_error (C++11)
概要
regex_errorクラスは、正規表現ライブラリ<regex>からのエラー報告として送出される、例外オブジェクトの型である。
有効ではない正規表現が入力された場合に送出される。
エラーとなった理由は、what() メンバ関数によってエラーメッセージ文字列として取得できるほか、code() メンバ関数によってregex_constants::error_type 型のエラーコード値としても取得できる。
メンバ関数
基底クラスであるruntime_error も参照のこと。
構築・破棄
名前
説明
対応バージョン
(constructor)
コンストラクタ
C++11
~regex_error() = default;
デストラクタ
C++11
regex_error& operator=(const regex_error&) = default; regex_error& operator=(regex_error&&) = default;
代入演算子
C++11
エラー内容
名前
説明
対応バージョン
code
エラーコードを取得する
C++11
例
#include <iostream>
#include <regex>
#include <string>
std::string code_to_string ( std::regex_constants::error_type e )
{
using namespace std :: regex_constants ;
switch ( e ) {
case error_collate : return "error collapse" ;
case error_ctype : return "error ctype" ;
case error_escape : return "error escape" ;
case error_backref : return "error back reference" ;
case error_brack : return "error bracket" ;
case error_paren : return "error paren" ;
case error_brace : return "error brace" ;
case error_badbrace : return "error bad brace" ;
case error_range : return "error range " ;
case error_space : return "error space" ;
case error_badrepeat : return "error bad repeat" ;
case error_complexity : return "error complexity" ;
case error_stack : return "error stack" ;
default :
throw std::invalid_argument ( "invalid error code" );
}
}
int main ()
{
try {
std::regex re ( "(" ); // 開きカッコに対応する閉じカッコがない正規表現を指定
}
catch ( std::regex_error & e ) {
std::cout << code_to_string ( e . code() ) << std::endl ;
std::cout << e . what () << std::endl ;
}
}
出力例
error paren
The expression contained mismatched ( and ).
バージョン
言語
処理系
Clang : 3.0 ✅ , 3.1 ✅ , 3.2 ✅ , 3.3 ✅ , 3.4 ✅ , 3.5 ✅ , 3.6 ✅
GCC : 4.9.0 ✅ , 4.9.1 ✅ , 5.0.0 ✅
Visual C++ : ?