basic_regex

template<class charT, class traits = regex_traits<charT>>
class basic_regex

テンプレートクラス basic_regex は、正規表現の解析とコンパイルをカプセル化する。このクラスは 2 つのテンプレート引数をとる。

テンプレートパラメータ
  • charT -- 文字型を決定する。すなわち charwchar_t のいずれかである。charT のコンセプトを見よ。

  • traits -- 例えばどの文字クラス名を考慮するか、といった文字型の振る舞いを決定する。既定の特性クラスとして regex_traits<charT> が用意されている。traits のコンセプトを見よ。

簡単に使用できるように、標準的な basic_regex インスタンスを定義する typedef が 2 つある。カスタムの特性クラスか非標準の文字型(例えば Unicode サポートを見よ)を使用するつもりがなければ、この 2 つだけを使用すればよい。

概要

#include <boost/regex.hpp>

namespace boost{

template <class charT, class traits = regex_traits<charT>  >
class basic_regex;

typedef basic_regex<char>      regex;
typedef basic_regex<wchar_t>   wregex;

}

以下が basic_regex の定義である。basic_string クラスに基づいており、charT の定数コンテナの要求事項を満足する。

template <class charT, class traits = regex_traits<charT> >
class basic_regex {
   public:
   // 型:
   typedef          charT                               value_type;
   typedef          implementation-specific             const_iterator;
   typedef          const_iterator                      iterator;
   typedef          charT&                              reference;
   typedef          const charT&                        const_reference;
   typedef          std::ptrdiff_t                      difference_type;
   typedef          std::size_t                         size_type;
   typedef          regex_constants::syntax_option_type flag_type;
   typedef typename traits::locale_type                 locale_type;

   // 定数:
   // メインオプションの選択:
   static const regex_constants:: syntax_option_type normal
                                                = regex_constants::normal;
   static const regex_constants:: syntax_option_type ECMAScript
                                                = normal;
   static const regex_constants:: syntax_option_type JavaScript
                                                = normal;
   static const regex_constants:: syntax_option_type Jscript
                                                = normal;
   static const regex_constants:: syntax_option_type basic
                                                = regex_constants::basic;
   static const regex_constants:: syntax_option_type extended
                                                = regex_constants::extended;
   static const regex_constants:: syntax_option_type awk
                                                = regex_constants::awk;
   static const regex_constants:: syntax_option_type grep
                                                = regex_constants::grep;
   static const regex_constants:: syntax_option_type egrep
                                                = regex_constants::egrep;
   static const regex_constants:: syntax_option_type sed
                                                = basic = regex_constants::sed;
   static const regex_constants:: syntax_option_type perl
                                                = regex_constants::perl;
   static const regex_constants:: syntax_option_type literal
                                                = regex_constants::literal;

   // Perl 正規表現固有の修飾子:
   static const regex_constants:: syntax_option_type no_mod_m
                                                = regex_constants::no_mod_m;
   static const regex_constants:: syntax_option_type no_mod_s
                                                = regex_constants::no_mod_s;
   static const regex_constants:: syntax_option_type mod_s
                                                = regex_constants::mod_s;
   static const regex_constants:: syntax_option_type mod_x
                                                = regex_constants::mod_x;

   // POSIX 基本正規表現固有の修飾子:
   static const regex_constants:: syntax_option_type bk_plus_qm
                                                = regex_constants::bk_plus_qm;
   static const regex_constants:: syntax_option_type bk_vbar
                                                = regex_constants::bk_vbar;
   static const regex_constants:: syntax_option_type no_char_classes
                                                = regex_constants::no_char_classes;
   static const regex_constants:: syntax_option_type no_intervals
                                                = regex_constants::no_intervals;

   // 共通の修飾子:
   static const regex_constants:: syntax_option_type nosubs
                                                = regex_constants::nosubs;
   static const regex_constants:: syntax_option_type optimize
                                                = regex_constants::optimize;
   static const regex_constants:: syntax_option_type collate
                                                = regex_constants::collate;
   static const regex_constants:: syntax_option_type newline_alt
                                                = regex_constants::newline_alt;
   static const regex_constants:: syntax_option_type no_except
                                                = regex_constants::newline_alt;

   // 構築、コピー、解体:
   explicit basic_regex ();
   explicit basic_regex(const charT* p, flag_type f = regex_constants::normal);
   basic_regex(const charT* p1, const charT* p2,
               flag_type f = regex_constants::normal);
   basic_regex(const charT* p, size_type len, flag_type f);
   basic_regex(const basic_regex&);

   template <class ST, class SA>
   explicit basic_regex(const basic_string<charT, ST, SA>& p,
                        flag_type f = regex_constants::normal);

   template <class InputIterator>
   basic_regex(InputIterator first, InputIterator last,
               flag_type f = regex_constants::normal);

   ~basic_regex();
   basic_regex& operator=(const basic_regex&);
   basic_regex& operator= (const charT* ptr);

   template <class ST, class SA>
   basic_regex& operator= (const basic_string<charT, ST, SA>& p);
   // イテレータ:
   std::pair<const_iterator, const_iterator> subexpression(size_type n) const;
   const_iterator begin() const;
   const_iterator end() const;
   // 容量:
   size_type size() const;
   size_type max_size() const;
   bool empty() const;
   size_type mark_count()const;
   //
   // 変更:
   basic_regex& assign(const basic_regex& that);
   basic_regex& assign(const charT* ptr,
                       flag_type f = regex_constants::normal);
   basic_regex& assign(const charT* ptr, unsigned int len, flag_type f);

   template <class string_traits, class A>
   basic_regex& assign(const basic_string<charT, string_traits, A>& s,
                       flag_type f = regex_constants::normal);

   template <class InputIterator>
   basic_regex& assign(InputIterator first, InputIterator last,
                       flag_type f = regex_constants::normal);

   // const な操作:
   flag_type flags() const;
   int status()const;
   basic_string<charT> str() const;
   int compare(basic_regex&) const;
   // ロカール:
   locale_type imbue(locale_type loc);
   locale_type getloc() const;
   // 値の交換
   void swap(basic_regex&) throw();
};

template <class charT, class traits>
bool operator == (const basic_regex<charT, traits>& lhs,
                  const basic_regex<charT, traits>& rhs);

template <class charT, class traits>
bool operator != (const basic_regex<charT, traits>& lhs,
                  const basic_regex<charT, traits>& rhs);

template <class charT, class traits>
bool operator < (const basic_regex<charT, traits>& lhs,
                  const basic_regex<charT, traits>& rhs);

template <class charT, class traits>
bool operator <= (const basic_regex<charT, traits>& lhs,
                  const basic_regex<charT, traits>& rhs);

template <class charT, class traits>
bool operator >= (const basic_regex<charT, traits>& lhs,
                  const basic_regex<charT, traits>& rhs);

template <class charT, class traits>
bool operator > (const basic_regex<charT, traits>& lhs,
                 const basic_regex<charT, traits>& rhs);

template <class charT, class io_traits, class re_traits>
basic_ostream<charT, io_traits>&
operator << (basic_ostream<charT, io_traits>& os,
             const basic_regex<charT, re_traits>& e);

template <class charT, class traits>
void swap(basic_regex<charT, traits>& e1,
          basic_regex<charT, traits>& e2);

typedef basic_regex<char> regex;
typedef basic_regex<wchar_t> wregex;

} // namespace boost

説明

basic_regex クラスは以下の公開メンバをもつ。

// メインオプションの選択:
static const regex_constants:: syntax_option_type normal
                                           = regex_constants::normal;
static const regex_constants:: syntax_option_type ECMAScript
                                           = normal;
static const regex_constants:: syntax_option_type JavaScript
                                           = normal;
static const regex_constants:: syntax_option_type Jscript
                                           = normal;
static const regex_constants:: syntax_option_type basic
                                           = regex_constants::basic;
static const regex_constants:: syntax_option_type extended
                                           = regex_constants::extended;
static const regex_constants:: syntax_option_type awk
                                           = regex_constants::awk;
static const regex_constants:: syntax_option_type grep
                                           = regex_constants::grep;
static const regex_constants:: syntax_option_type egrep
                                           = regex_constants::egrep;
static const regex_constants:: syntax_option_type sed
                                           = basic = regex_constants::sed;
static const regex_constants:: syntax_option_type perl
                                           = regex_constants::perl;
static const regex_constants:: syntax_option_type literal
                                           = regex_constants::literal;

// Perl 正規表現固有の修飾子:
static const regex_constants:: syntax_option_type no_mod_m
                                           = regex_constants::no_mod_m;
static const regex_constants:: syntax_option_type no_mod_s
                                           = regex_constants::no_mod_s;
static const regex_constants:: syntax_option_type mod_s
                                           = regex_constants::mod_s;
static const regex_constants:: syntax_option_type mod_x
                                           = regex_constants::mod_x;

// POSIX 基本正規表現固有の修飾子:
static const regex_constants:: syntax_option_type bk_plus_qm
                                           = regex_constants::bk_plus_qm;
static const regex_constants:: syntax_option_type bk_vbar
                                           = regex_constants::bk_vbar;
static const regex_constants:: syntax_option_type no_char_classes
                                           = regex_constants::no_char_classes;
static const regex_constants:: syntax_option_type no_intervals
                                           = regex_constants::no_intervals;

// 共通の修飾子:
static const regex_constants:: syntax_option_type nosubs
                                           = regex_constants::nosubs;
static const regex_constants:: syntax_option_type optimize
                                           = regex_constants::optimize;
static const regex_constants:: syntax_option_type collate
                                           = regex_constants::collate;
static const regex_constants:: syntax_option_type newline_alt
                                           = regex_constants::newline_alt;
static const regex_constants:: syntax_option_type no_except
                                           = regex_constants::newline_alt;

これらのオプションの意味は syntax_option_type の節にある。

静的定数メンバは名前空間 boost::regex_constants 内で宣言した定数の別名として提供している。名前空間 boost::regex_constants 内で宣言されている syntax_option_type 型の各定数については、basic_regex のスコープで同じ名前・型・値で宣言している。

basic_regex()
効果

basic_regex クラスのオブジェクトを構築する。

basic_regex デフォルトコンストラクタの事後条件

要素

empty()

true

size()

0

str()

basic_string<charT>()

basic_regex(const chartT *p, flag_type f = regex_constants::normal)
要件

p は null ポインタ以外。

例外

bad_expression -- s が正しい正規表現でない場合(f にフラグ no_except が設定されていない場合)。

効果

basic_regex クラスのオブジェクトを構築する。f で指定したオプションフラグにしたがって null 終端文字列 p の正規表現を解釈し、オブジェクトの内部有限状態マシンを構築する。

basic_regex デフォルトコンストラクタの事後条件

要素

empty()

false

size()

char_traits<charT>::length(p)

str()

basic_string<charT>(p)

flags()

f

mark_count()

正規表現中に含まれるマーク済み部分式の総数

basic_regex(const charT *p1, const charT *p2, flag_type f = regex_constants::normal)
要件

p1p2 は null ポインタ以外、かつ p1 < p2

例外

bad_expression -- [p1,p2) が正しい正規表現でない場合(fno_except が設定されていない場合)。

効果

クラス basic_regex のオブジェクトを構築する。f で指定したオプションフラグにしたがって文字シーケンス [p1,p2) の正規表現を解釈し、オブジェクトの内部有限状態マシンを構築する。

basic_regex デフォルトコンストラクタの事後条件

要素

empty()

false

size()

std::distance(p1, p2)

str()

basic_string<charT>(p1, p2)

flags()

f

mark_count()

正規表現中に含まれるマーク済み部分式の総数

basic_regex(const charT *p, size_type len, flag_type f)
要件

p は null ポインタ以外、かつ len < max_size()

例外

bad_expression -- p が正しい正規表現でない場合(fno_except が設定されていない場合)。

効果

クラス basic_regex のオブジェクトを構築する。f で指定したオプションフラグにしたがって文字シーケンス [p,p+len) の正規表現を解釈し、オブジェクトの内部有限状態マシンを構築する。

basic_regex デフォルトコンストラクタの事後条件

要素

empty()

false

size()

len

str()

basic_string<charT>(p, len)

flags()

f

mark_count()

正規表現中に含まれるマーク済み部分式の総数

basic_regex(const basic_regex &e)
効果

オブジェクト e をコピーしてクラス basic_regex オブジェクトを構築する。

template<class ST, class SA>
basic_regex(const basic_string<charT, ST, SA> &s, type_flag f = regex_constants::normal)
例外

bad_expression -- s が正しい正規表現でない場合(fno_except が設定されていない場合)。

効果

basic_regex クラスのオブジェクトを構築する。f で指定したオプションフラグにしたがって文字列 s の正規表現を解釈し、オブジェクトの内部有限状態マシンを構築する。

basic_regex コンストラクタの事後条件

要素

empty()

false

size()

s.size()

str()

s

flags()

f

mark_count()

正規表現中に含まれるマーク済み部分式の総数

template<class ForwardIterator>
basic_regex(ForwardIterator first, ForwardIterator last, flag_type f = regex_constants::normal)
例外

bad_expression -- [first,last) が正しい正規表現でない場合(fno_except が設定されていない場合)。

効果

basic_regex クラスのオブジェクトを構築する。f で指定したオプションフラグにしたがって文字シーケンス [first,last) の正規表現を解釈し、オブジェクトの内部有限状態マシンを構築する。

basic_regex コンストラクタの事後条件

要素

empty()

false

size()

distance(first, last)

str()

basic_string<charT>(first, last)

flags()

f

mark_count()

正規表現中に含まれるマーク済み部分式の総数

basic_regex &operator=(const basic_regex &e)
効果

assign(e.str(), e.flags()) の結果を返す。

basic_regex &operator=(const charT *ptr)
要件

ptr は null ポインタ以外。

効果

assign(ptr) の結果を返す。

template<class ST, class SA>
basic_regex &operator=(const basic_regex<charT, ST, SA> &p)
効果

assign(p) の結果を返す。

std::pair<const_iterator, const_iterator> subexpression(size_type n) const
効果

元の正規表現文字列内のマーク済み部分式 n の位置を表すイテレータのペアを返す。戻り値のイテレータは begin および end からの相対位置である。

要件

正規表現は syntax_option_type save_subexpression_location を設定してコンパイルしていなければならない。引数 n0 <= n < mark_count() の範囲になければならない。

const_iterator begin() const
効果

正規表現を表す文字シーケンスの開始イテレータを返す。

const_iterator end() const
効果

正規表現を表す文字シーケンスの終了イテレータを返す。

size_type size() const
効果

正規表現を表す文字シーケンスの長さを返す。

size_type max_size() const
効果

正規表現を表す文字シーケンスの最大長さを返す。

bool empty() const
効果

オブジェクトが正しい正規表現を保持していない場合に真を返す。それ以外の場合は偽を返す。

unsigned mark_count() const
効果

正規表現中のマーク済み部分式の数を返す。

basic_regex &assign(const basic_regex &that)
効果

assign(that.str(), that.flags()) を返す。

basic_regex &assign(const charT *ptr, flag_type f)
効果

assign(string_type(ptr), f) を返す。

basic_regex &assign(const charT *ptr, unsigned int len, flag_type f)
効果

assign(string_type(ptr, len), f) を返す。

template<class string_traits, class A>
basic_regex &assign(const basic_string<charT, string_traits, A> &s, flag_type f)
例外

bad_expression -- s が正しい正規表現でない場合(fno_except が設定されていない場合)。

戻り値

*this

効果

f で指定したオプションフラグにしたがって文字列 s の正規表現を解釈し代入する。

basic_regex::assign の事後条件

要素

empty()

false

size()

s.size()

str()

s

flags()

f

mark_count

正規表現中に含まれるマーク済み部分式の総数

template<class InputIterator>
basic_regex &assign(InputIterator first, InputIterator last, flag_type f)
要件

InputIterator 型は入力イテレータの要件(24.1.1)を満たす。

効果

assign(string_type(first, last), f) を返す。

flag_type flags() const
効果

オブジェクトのコンストラクタ、あるいは最後の assign の呼び出しで渡した正規表現構文のフラグのコピーを返す。

int status() const
効果

正規表現が正しい正規表現であれば 0、それ以外の場合はエラーコードを返す。このメンバ関数は例外処理を使用できない環境のために用意されている。

basic_string<charT> str() const
効果

オブジェクトのコンストラクタ、あるいは最後の assign の呼び出しで渡した文字シーケンスのコピーを返す。

int compare(basic_regex &e) const
効果

flags() == e.flags() であれば str().compare(e.str()) を、それ以外の場合は flags() - e.flags() を返す。

locale_type imbue(locale_type l)
効果

traits_inst.imbue(l) の結果を返す。traits_inst はオブジェクト内の、テンプレート引数 traits のインスタンス(をデフォルトコンストラクタで初期化したもの)である。

事後条件

empty() == true

locale_type getloc() const
効果

traits_inst.getloc() の結果を返す。traits_inst はオブジェクト内の、テンプレート引数 traits のインスタンス(をデフォルトコンストラクタで初期化したもの)である。

void swap(basic_regex &e) noexcept
効果

2 つの正規表現の内容を交換する。

事後条件

*thise にあった正規表現を保持し、e*this にあった正規表現を保持する。

計算量

一定。

template<class charT, class traits>
bool operator==(const basic_regex<charT, traits> &lhs, const basic_regex<charT, traits> &rhs)

注釈

basic_regex オブジェクト間の比較は実験的なものである。Technical Report on C++ Libraries には記述がなく、basic_regex の他の実装に移植する必要がある場合は注意していただきたい。

効果

lhs.compare(rhs) == 0 を返す。

template<class charT, class traits>
bool operator!=(const basic_regex<charT, traits> &lhs, const basic_regex<charT, traits> &rhs)
効果

lhs.compare(rhs) != 0 を返す。

template<class charT, class traits>
bool operator<(const basic_regex<charT, traits> &lhs, const basic_regex<charT, traits> &rhs)
効果

lhs.compare(rhs) < 0 を返す。

template<class charT, class traits>
bool operator<=(const basic_regex<charT, traits> &lhs, const basic_regex<charT, traits> &rhs)
効果

lhs.compare(rhs) <= 0 を返す。

template<class charT, class traits>
bool operator>=(const basic_regex<charT, traits> &lhs, const basic_regex<charT, traits> &rhs)
効果

lhs.compare(rhs) >= 0 を返す。

template<class charT, class traits>
bool operator>(const basic_regex<charT, traits> &lhs, const basic_regex<charT, traits> &rhs)
効果

lhs.compare(rhs) > 0 を返す。

template<class charT, class io_traits, class re_traits>
basic_ostream<charT, io_traits> &operator<<(basic_ostream<charT, io_traits> &os, const basic_regex<charT, re_traits> &e)

注釈

basic_regex のストリーム挿入子は実験的なものであり、正規表現のテキスト表現をストリームに出力する。

効果

(os << e.str()) を返す。

void swap(basic_regex<charT, traits> &lhs, basic_regex<charT, traits> &rhs)
効果

lhs.swap(rhs) を呼び出す。