basic_regex
-
template<class charT, class traits = regex_traits<charT>>
class basic_regex テンプレートクラス
basic_regex
は、正規表現の解析とコンパイルをカプセル化する。このクラスは 2 つのテンプレート引数をとる。- テンプレートパラメータ
charT -- 文字型を決定する。すなわち
char
かwchar_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; // 構築、コピー、解体: explicitbasic_regex
(); explicitbasic_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> explicitbasic_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_iteratorbegin
() const; const_iteratorend
() const; // 容量: size_typesize
() const; size_typemax_size
() const; boolempty
() const; size_typemark_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_typeflags
() const; intstatus
()const; basic_string<charT>str
() const; intcompare
(basic_regex&) const; // ロカール: locale_typeimbue
(locale_type loc); locale_typegetloc
() const; // 値の交換 voidswap
(basic_regex&) throw(); }; template <class charT, class traits> booloperator ==
(const basic_regex<charT, traits>& lhs, const basic_regex<charT, traits>& rhs); template <class charT, class traits> booloperator !=
(const basic_regex<charT, traits>& lhs, const basic_regex<charT, traits>& rhs); template <class charT, class traits> booloperator <
(const basic_regex<charT, traits>& lhs, const basic_regex<charT, traits>& rhs); template <class charT, class traits> booloperator <=
(const basic_regex<charT, traits>& lhs, const basic_regex<charT, traits>& rhs); template <class charT, class traits> booloperator >=
(const basic_regex<charT, traits>& lhs, const basic_regex<charT, traits>& rhs); template <class charT, class traits> booloperator >
(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> voidswap
(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
正規表現中に含まれるマーク済み部分式の総数
-
basic_regex(const charT *p1, const charT *p2, flag_type f = regex_constants::normal)
- 要件
- 例外
bad_expression -- [p1,p2) が正しい正規表現でない場合(
f
にno_except
が設定されていない場合)。- 効果
クラス
basic_regex
のオブジェクトを構築する。f
で指定したオプションフラグにしたがって文字シーケンス [p1,p2) の正規表現を解釈し、オブジェクトの内部有限状態マシンを構築する。
basic_regex
デフォルトコンストラクタの事後条件要素
値
empty()
false
size()
str()
flags()
f
正規表現中に含まれるマーク済み部分式の総数
-
basic_regex(const charT *p, size_type len, flag_type f)
- 要件
- 例外
bad_expression --
p
が正しい正規表現でない場合(f
にno_except
が設定されていない場合)。- 効果
クラス
basic_regex
のオブジェクトを構築する。f
で指定したオプションフラグにしたがって文字シーケンス [p,p+len) の正規表現を解釈し、オブジェクトの内部有限状態マシンを構築する。
basic_regex
デフォルトコンストラクタの事後条件要素
値
empty()
false
size()
len
str()
flags()
f
正規表現中に含まれるマーク済み部分式の総数
-
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
が正しい正規表現でない場合(f
にno_except
が設定されていない場合)。- 効果
basic_regex
クラスのオブジェクトを構築する。f
で指定したオプションフラグにしたがって文字列s
の正規表現を解釈し、オブジェクトの内部有限状態マシンを構築する。
basic_regex
コンストラクタの事後条件要素
値
empty()
false
size()
s.size()
str()
s
flags()
f
正規表現中に含まれるマーク済み部分式の総数
-
template<class ForwardIterator>
basic_regex(ForwardIterator first, ForwardIterator last, flag_type f = regex_constants::normal) - 例外
bad_expression -- [first,last) が正しい正規表現でない場合(
f
にno_except
が設定されていない場合)。- 効果
basic_regex
クラスのオブジェクトを構築する。f
で指定したオプションフラグにしたがって文字シーケンス [first,last) の正規表現を解釈し、オブジェクトの内部有限状態マシンを構築する。
basic_regex
コンストラクタの事後条件要素
値
empty()
false
size()
str()
flags()
f
正規表現中に含まれるマーク済み部分式の総数
-
std::pair<const_iterator, const_iterator> subexpression(size_type n) const
- 効果
元の正規表現文字列内のマーク済み部分式
n
の位置を表すイテレータのペアを返す。戻り値のイテレータはbegin
およびend
からの相対位置である。- 要件
正規表現は
syntax_option_type
save_subexpression_location
を設定してコンパイルしていなければならない。引数n
は0 <= 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 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) - 例外
bad_expression --
s
が正しい正規表現でない場合(f
にno_except
が設定されていない場合)。- 戻り値
*this。
- 効果
f
で指定したオプションフラグにしたがって文字列s
の正規表現を解釈し代入する。
basic_regex::assign
の事後条件要素
値
empty()
false
size()
s.size()
str()
s
flags()
f
正規表現中に含まれるマーク済み部分式の総数
-
template<class InputIterator>
basic_regex &assign(InputIterator first, InputIterator last, flag_type f) - 要件
InputIterator
型は入力イテレータの要件(24.1.1)を満たす。- 効果
-
flag_type flags() const
- 効果
オブジェクトのコンストラクタ、あるいは最後の
assign
の呼び出しで渡した正規表現構文のフラグのコピーを返す。
-
int status() const
- 効果
正規表現が正しい正規表現であれば 0、それ以外の場合はエラーコードを返す。このメンバ関数は例外処理を使用できない環境のために用意されている。
-
basic_string<charT> str() const
- 効果
オブジェクトのコンストラクタ、あるいは最後の
assign
の呼び出しで渡した文字シーケンスのコピーを返す。
-
int compare(basic_regex &e) const
-
locale_type imbue(locale_type l)
-
locale_type getloc() const
- 効果
traits_inst.getloc() の結果を返す。
traits_inst
はオブジェクト内の、テンプレート引数traits
のインスタンス(をデフォルトコンストラクタで初期化したもの)である。
-
void swap(basic_regex &e) noexcept
-
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
の他の実装に移植する必要がある場合は注意していただきたい。
-
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) 注釈
basic_regex
のストリーム挿入子は実験的なものであり、正規表現のテキスト表現をストリームに出力する。
-
void swap(basic_regex<charT, traits> &lhs, basic_regex<charT, traits> &rhs)