C++ Boost

Boost.Regex

basic_regex

Boost.Regex Index




Synopsis

#include <boost/regex.hpp>

テンプレートクラスbasic_regexは正規表現のパースとコンパイルをカプセル化している. このクラスは3つのテンプレートパラメータを取る:

charT: 文字型を決定する.つまりcharかwchart.

traits:文字型の振る舞いを決定する.例えば文字クラス名が認識されるか,など. 既定の特性クラスはregex_traits<charT>

Allocator: このクラスがメモリ割り当てに使うアロケータクラス.

手軽に使うために二つのtypedefがあり,二つの標準的なbasic_regexの実体を定義している. 独自の特性クラスやアロケータを使わないでよければ,以下のものだけ使えば充分だ.

namespace boost{
template <class charT, class traits = regex_traits<charT>, class Allocator = std::allocator<charT>  >
class basic_regex;
typedef basic_regex<char> regex;
typedef basic_regex<wchar_t> wregex;
}

basic_regexの定義は以下のとおり: basicstringクラスに基づいていて, charTの定数コンテナに対する要求を満たしている.

namespace boost{

template <class charT,
class traits = regex_traits<charT>,
class Allocator = allocator<charT> >
class basic_regex
{
public:
// types:
typedef          charT                                value_type;
typedef          implementation defined               const_iterator;
typedef          const_iterator                       iterator;
typedef typename Allocator::reference                 reference;
typedef typename Allocator::const_reference           const_reference;
typedef typename Allocator::difference_type           difference_type;
typedef typename Allocator::size_type                 size_type;
typedef          Allocator                            allocator_type;
typedef          regex_constants::syntax_option_type  flag_type;
typedef typename traits::locale_type                  locale_type;

// constants:
static const regex_constants::syntax_option_type normal = regex_constants::normal;
static const regex_constants::syntax_option_type icase = regex_constants::icase;
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 ECMAScript = normal;
static const regex_constants::syntax_option_type JavaScript = normal;
static const regex_constants::syntax_option_type JScript = normal;
// these flags are optional, if the functionality is supported
// then the flags shall take these names.
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;

// construct/copy/destroy:
explicit basic_regex(const Allocator& a = Allocator());
explicit basic_regex(const charT* p, flag_type f = regex_constants::normal,
const Allocator& a = Allocator());
basic_regex(const charT* p1, const charT* p2, flag_type f = regex_constants::normal,
const Allocator& a = Allocator());
basic_regex(const charT* p, size_type len, flag_type f,
const Allocator& a = Allocator());
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,
const Allocator& a = Allocator());
template <class InputIterator>
basic_regex(InputIterator first, inputIterator last,
flag_type f = regex_constants::normal,
const Allocator& a = Allocator());

~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);
// iterators: 
const_iterator begin() const; 
const_iterator end() const;
// capacity: 
size_type size() const; 
size_type max_size() const; 
bool empty() const; 
unsigned mark_count()const; 
//
// modifiers: 
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 operations:
Allocator get_allocator() const;
flag_type flags() const;
basic_string<charT> str() const;
int compare(basic_regex&) const;
// locale:
locale_type imbue(locale_type loc);
locale_type getloc() const;
// swap
void swap(basic_regex&) throw();
};

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

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

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

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

} // namespace boost

Description

basic_regexクラスには以下の公開メンバ関数がある:

basic_regex constants

static const regex_constants::syntax_option_type normal = regex_constants::normal;
static const regex_constants::syntax_option_type icase = regex_constants::icase;
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 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;

スタティック定数メンバはboost::regex_constants名前空間で宣言された定数の同義語として与えられている. boost::regex_constants名前空間で宣言された syntax_option_type型の定数

basic_regex constructors

basic_regexの全てのコンストラクタで, オブジェクトの存在期間にコンストラクタかメンバ関数によって行われるあらゆるメモリ割り当てのために, Allocator引数のコピーが使われる.

basic_regex(const Allocator& a = Allocator());

Effects: basic_regexオブジェクトの構築.この関数の事後条件は以下の通り.

Element

Value

empty()

true

size()

0

str()

basic_string<charT>()

 basic_regex(const charT* p, flag_type f = regex_constants::normal, const Allocator& a = Allocator());

Requires: pはヌルポではならない.

Throws: pが有効な正規表現でなければbad_expression例外が投げられる.

Effects: basic_regexクラスのオブジェクトを構築する. ヌル終端文字列の正規表現からオブジェクト内部の有限状態マシンが構築され, fで指定されたオプションフラグに基づいて解釈される. 事後条件は以下の通り:

Element

Value

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, const Allocator& a = Allocator());

Requires: p1p2はヌルポであってはならない.p1 < p2

Throws: [p1,p2)が有効な正規表現でなければbad_expression例外が投げられる.

Effects: basic_regexクラスのオブジェクトを構築する. 文字シーケンス[p1,p2)の正規表現からオブジェクト内部の有限状態マシンが構築され, fで指定されたオプションフラグに基づいて解釈される. 事後条件は以下の通り:

Element

Value

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, const Allocator& a = Allocator());

Requires: pはヌルポであってはならない.len < max_size()

Throws: pが有効な正規表現でなければbad_expression例外が投げられる.

Effects: basic_regexクラスのオブジェクトを構築する. 文字シーケンス[p1,p+len)の正規表現からオブジェクト内部の有限状態マシンが構築され, fで指定されたオプションフラグに基づいて解釈される. 事後条件は以下の通り:

Element

Value

empty()

false

size()

len

str()

basic_string<charT>(p, len)

flags()

f

mark_count()

正規表現の中のマークされた子表現の数



basic_regex(const basic_regex& e);

Effects: オブジェクトeのコピーとしてbasic_regexクラスのオブジェクトを構築する.事後条件は以下の通り:

Element

Value

empty()

e.empty()

size()

e.size()

str()

e.str()

flags()

e.flags()

mark_count()

e.mark_count()


template <class ST, class SA>

basic_regex(const basic_string<charT, ST, SA>& s,
flag_type f = regex_constants::normal, const Allocator& a = Allocator());

Throws: sが有効な正規表現でなければbad_expression例外が投げられる.

Effects: basic_regexクラスのオブジェクトを構築する. 文字列sの正規表現からオブジェクト内部の有限状態マシンが構築され, fで指定されたオプションフラグに基づいて解釈される. 事後条件は以下の通り:

Element

Value

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, const Allocator& a = Allocator());

Throws: シーケンス[first,last)が有効な正規表現でなければbad_expression例外が投げられる.

Effects: basic_regexクラスのオブジェクトを構築する. 文字シーケンス[first,last)の正規表現からオブジェクト内部の有限状態マシンが構築され, fで指定されたオプションフラグに基づいて解釈される. 事後条件は以下の通り:

Element

Value

empty()

false

size()

distance(first,last)

str()

basic_string<charT>(first,last)

flags()

f

mark_count()

正規表現の中のマークされた子表現の数



basic_regex& operator=(const basic_regex& e);

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


basic_regex& operator=(const charT* ptr);

Requires: pはヌルポであってはならない.

Effects: assign(ptr)の結果を返す.


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

Effects: assign(p)の結果を返す.

basic_regex iterators


const_iterator begin() const;

Effects: 正規表現を表す文字シーケンスの始点イテレータを返す.


const_iterator end() const;

Effects: 正規表現を表す文字シーケンスの終端イテレータを返す.

basic_regex capacity


size_type size() const;

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


size_type max_size() const;

Effects: 正規表現を表す文字シーケンスの長さの上限を返す.


bool empty() const;

Effects: オブジェクトが有効な正規表現を保持していればtrueを,そうでなければfalseを返す.

unsigned mark_count() const;

Effects: 正規表現の中のマークされた子表現の数を返す.

basic_regex assign


basic_regex& assign(const basic_regex& that);

Effects: assign(that.str(), that.flags())を返す.


basic_regex& assign(const charT* ptr, flag_type f = regex_constants::normal);

Effects: assign(string_type(ptr), f)を返す.

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

Effects: 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 = regex_constants::normal);

Throws: sが有効な正規表現でなければbad_expression例外を投げる.

Returns: *this.

Effects: fで指定されたオプションフラグに基づいて解釈される, 文字列sの正規表現を代入する.事後条件は以下の通り:

Element

Value

empty()

false

size()

s.size()

str()

s

flags()

f

mark_count()

正規表現の中のマークされた子表現の数を返す.


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

Requires: InputIterator型は入力イテレータの要求(24.1.1)に対応すること.

Effects: assign(string_type(first, last), f)を返す.

basic_regex constant operations

Allocator get_allocator() const;

Effects: オブジェクトのコンストラクタに渡されたAllocatorのコピーを返す.

flag_type flags() const;

Effects: オブジェクトのコンストラクタ, またはassignの最後の呼び出しに渡された正規表現の文法フラグのコピーを返す.

basic_string<charT> str() const;

Effects: オブジェクトのコンストラクタ, またはassignの最後の呼び出しに渡された文字シーケンスのコピーを返す.

basic_string<charT> str() const;
int compare(basic_regex& e)const;

Effects: flags() == e.flags() であれば, str().compare(e.str())を返す. そうでなければ flags() - e.flags()を返す.

basic_regex locale

locale_type imbue(locale_type l);

Effects: traits_inst.imbue(l)の結果を返す. traits_instは(既定の初期化では)オブジェクトが保持するテンプレートパラメータtraits の実体である.imbueの呼び出しは保持されている正規表現を無効化する.

Postcondition: empty() == true.


locale_type getloc() const;

Effects: traits_inst.getloc()の結果を返す. traits_instは(既定の初期化では)オブジェクトが保持するテンプレートパラメータtraits の実体である.

basic_regex swap


void swap(basic_regex& e) throw();

Effects: 二つの正規表現の内容を入れ替える.

Postcondition: *thiseの正規表現を保持する. e*thisが保持していた正規表現を保持する.

Complexity: 定数時間.

basic_regex 非メンバ関数

basic_regex 非メンバ比較演算子

basic_regex オブジェクト同士の比較は実験的な土台の上に提供されている: これらは標準ライブラリ提案には含まれないかもしれないので, 移植性のあるコードを書くなら気をつけてほしい.


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

Effects: lhs.compare(rhs) == 0を返す.


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

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


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

Effects: lhs.compare(rhs) < 0を返す.


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

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


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

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


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

Effects: lhs.compare(rhs) > 0を返す.

basic_regex 挿入子(inserter)

basic_regex ストリーム挿入子は実験的な土台の上に提供されていて, 正規表現をテキストとしてストリームに出力するものである.


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

Effects: (os << e.str()) を返す.

basic_regex 非メンバスワップ

template <class charT, class traits, class Allocator>
void swap(basic_regex<charT, traits, Allocator>& lhs,
basic_regex<charT, traits, Allocator>& rhs);

Effects: lhs.swap(rhs) を呼び出す.


Revised 24 Oct 2003

ゥ Copyright John Maddock 1998- 2003

Use, modification and distribution are subject to the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)