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;
// 構築、コピー、解体:
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_typenormal = regex_constants::normal; static const regex_constants::syntax_option_typeECMAScript = normal; static const regex_constants::syntax_option_typeJavaScript = normal; static const regex_constants::syntax_option_typeJscript = normal; static const regex_constants::syntax_option_typebasic = regex_constants::basic; static const regex_constants::syntax_option_typeextended = regex_constants::extended; static const regex_constants::syntax_option_typeawk = regex_constants::awk; static const regex_constants::syntax_option_typegrep = regex_constants::grep; static const regex_constants::syntax_option_typeegrep = regex_constants::egrep; static const regex_constants::syntax_option_typesed = basic = regex_constants::sed; static const regex_constants::syntax_option_typeperl = regex_constants::perl; static const regex_constants::syntax_option_typeliteral = regex_constants::literal; // Perl 正規表現固有の修飾子: static const regex_constants::syntax_option_typeno_mod_m = regex_constants::no_mod_m; static const regex_constants::syntax_option_typeno_mod_s = regex_constants::no_mod_s; static const regex_constants::syntax_option_typemod_s = regex_constants::mod_s; static const regex_constants::syntax_option_typemod_x = regex_constants::mod_x; // POSIX 基本正規表現固有の修飾子: static const regex_constants::syntax_option_typebk_plus_qm = regex_constants::bk_plus_qm; static const regex_constants::syntax_option_typebk_vbar = regex_constants::bk_vbar; static const regex_constants::syntax_option_typeno_char_classes = regex_constants::no_char_classes; static const regex_constants::syntax_option_typeno_intervals = regex_constants::no_intervals; // 共通の修飾子: static const regex_constants::syntax_option_typenosubs = regex_constants::nosubs; static const regex_constants::syntax_option_typeoptimize = regex_constants::optimize; static const regex_constants::syntax_option_typecollate = regex_constants::collate; static const regex_constants::syntax_option_typenewline_alt = regex_constants::newline_alt; static const regex_constants::syntax_option_typeno_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()
truesize()
0str()
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()
falsesize()
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()
falsesize()
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()
falsesize()
lenstr()
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()
falsesize()
s.size()
str()
sflags()
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()
falsesize()
str()
flags()
f正規表現中に含まれるマーク済み部分式の総数
-
std::pair<const_iterator, const_iterator> subexpression(size_type n) const
- 効果
元の正規表現文字列内のマーク済み部分式
nの位置を表すイテレータのペアを返す。戻り値のイテレータはbeginおよびendからの相対位置である。- 要件
正規表現は
syntax_option_typesave_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()
falsesize()
s.size()
str()
sflags()
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)