C++ Boost

Boost.Regex

class match_results

Boost.Regex Index




Contents

Synopsis
Description

Synopsis

#include <boost/regex.hpp>

正規表現は、多くの単純なパターンマッチングのアルゴリズムとは異なり, 全体のマッチを発見し,さらに子表現のマッチを得ることも出来る: それぞれの子表現は、パターン中の丸括弧 (...) の組により区切られている. ユーザが子表現マッチを得る方法がいくつかあり, 子表現マッチのインデクス化された集合としての機能をもつ match_results クラス により実現されている. それぞれの子表現マッチは sub_match 型のオブジェクトが保持している.

テンプレートクラス match_results は正規表現マッチの結果を表す文字シーケンス集合を示す. match_results 型のオブジェクトは,アルゴリズム regex_matchregex_searchに渡され,イテレータ regex_iteratorがこれを返す. 集合のストレージの割り当てと開放は必要に応じて match_results クラスのメンバ関数が行う.

テンプレートクラス match_results は(lib.sequences.reqmts)で明示された Sequence の要求を満たす. 但し const修飾されたシーケンスに対して定義された演算子だけがサポートされているという点は除く.

クラステンプレート match_results は cmatch, wcmatch, smatch, wsmatch という typedef のひとつとして,よく使われる.

template <class BidirectionalIterator,
class Allocator = allocator<sub_match<BidirectionalIterator> >
class match_results;

typedef match_results<const char*> cmatch;
typedef match_results<const wchar_t*> wcmatch;
typedef match_results<string::const_iterator> smatch;
typedef match_results<wstring::const_iterator> wsmatch;

template <class BidirectionalIterator,
class Allocator = allocator<sub_match<BidirectionalIterator> >
class match_results
{ 
public: 
typedef          sub_match<BidirectionalIterator>                        value_type;
typedef          const value_type&                                       const_reference;
typedef          const_reference                                         reference;
typedef          implementation defined                                  const_iterator;
typedef          const_iterator                                          iterator;
typedef typename iterator_traits<BidirectionalIterator>::difference_type difference_type;
typedef typename Allocator::size_type                                    size_type;
typedef          Allocator                                               allocator_type;
typedef typename iterator_traits<BidirectionalIterator>::value_type      char_type;
typedef          basic_string<char_type>                                 string_type;

// construct/copy/destroy:
explicit match_results(const Allocator& a = Allocator());
match_results(const match_results& m);
match_results& operator=(const match_results& m); 
~match_results();

// size:
size_type size() const;
size_type max_size() const;
bool empty() const;
// element access:
difference_type length(int sub = 0) const;
difference_type position(unsigned int sub = 0) const;
string_type str(int sub = 0) const;
const_reference operator[](int n) const;

const_reference prefix() const;

const_reference suffix() const;
const_iterator begin() const;
const_iterator end() const;
// format:
template <class OutputIterator>
OutputIterator format(OutputIterator out,
const string_type& fmt,
match_flag_type flags = format_default) const;
string_type format(const string_type& fmt,
match_flag_type flags = format_default) const;

allocator_type get_allocator() const;
void swap(match_results& that);

#ifdef BOOST_REGEX_MATCH_EXTRA
typedef typename value_type::capture_sequence_type capture_sequence_type;
const capture_sequence_type& captures(std::size_t i)const;
#endif

};

template <class BidirectionalIterator, class Allocator>
bool operator == (const match_results<BidirectionalIterator, Allocator>& m1,
const match_results<BidirectionalIterator, Allocator>& m2);
template <class BidirectionalIterator, class Allocator>
bool operator != (const match_results<BidirectionalIterator, Allocator>& m1,
const match_results<BidirectionalIterator, Allocator>& m2);

template <class charT, class traits, class BidirectionalIterator, class Allocator>
basic_ostream<charT, traits>&
operator << (basic_ostream<charT, traits>& os,
const match_results<BidirectionalIterator, Allocator>& m);

template <class BidirectionalIterator, class Allocator>
void swap(match_results<BidirectionalIterator, Allocator>& m1,
match_results<BidirectionalIterator, Allocator>& m2);

Description

match_results コンストラクタ

全てのmatch_results コンストラクタで, オブジェクトの生存期間にコンストラクタやメンバ関数で行われるメモリ割り当てでは, Allocator引数のコピーが使われる.


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

Effects: match_resultsクラスのオブジェクトを構築する. この関数の事後条件は以下の通り:

Element

Value

empty()

true

size()

0

str()

basic_string<charT>()

 


match_results(const match_results& m);

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


match_results& operator=(const match_results& m);

Effects: m を *this に代入する.この関数の事後条件は以下の通り:

Element

Value

empty()

m.empty().

size()

m.size().

str(n)

n < m.size() である全ての整数に対して m.str(n)

prefix()

m.prefix().

suffix()

m.suffix().

(*this)[n]

n < m.size() である全ての整数に対して m[n]

length(n)

n < m.size() である全ての整数に対して m.length(n)

position(n)

n < m.size() である全ての整数に対して m.position(n)

match_results size


size_type size()const;

Effects: *this が保持する sub_match 要素の数を返す. つまり正規表現の中のマークされた子表現のうち,マッチしたものの数に1を加えたもの.


size_type max_size()const;

Effects: *this が保持できる sub_match 要素の最大数を返す.


bool empty()const;

Effects: size() == 0 を返す.

match_results 要素アクセス


difference_type length(int sub = 0)const;

Effects: sub-expressions sub の長さを返す.つまり,(*this)[sub].length()


difference_type position(unsigned int sub = 0)const;

Effects: sub-expression subの開始位置を返す. subがマッチしていなければ -1 を返す.


string_type str(int sub = 0)const;

Effects: sub-expression sub を文字列として返す: string_type((*this)[sub]).


const_reference operator[](int n) const;

Effects: マークされた子表現 n にマッチした文字シーケンスを表す sub_match オブジェクトの参照を返す. n == 0 なら,正規表現全体にマッチした文字シーケンスを表す sub_match オブジェクトの参照を返す. n が範囲外か,またはnがマッチしなかった子表現なら, matchedメンバがfalseの sub_match オブジェクトの参照を返す.


const_reference prefix()const;

Effects: マッチやサーチが行われた文字列の開始点から,発見されたマッチの始点までの文字シーケンスを表す sub_match オブジェクトの参照を返す.


const_reference suffix()const;

Effects: 発見されたマッチの終点から,マッチやサーチが行われた文字列の終端点までの文字シーケンスを表す sub_match オブジェクトの参照を返す.


const_iterator begin()const;

Effects: *this が保持するマークされた子表現のマッチ全てを列挙そる開始イテレータを返す.


const_iterator end()const;

Effects: *this が保持するマークされた子表現のマッチ全てを列挙そる終点イテレータを返す.

match_results 再フォーマット

template <class OutputIterator>
OutputIterator format(OutputIterator out,
const string_type& fmt,
match_flag_type flags = format_default);

Requires: OutputIterator 型は 出力イテレータの要求(24.1.2)を満たす.

Effects: 文字シーケンス [fmt.begin(), fmt.end()) を OutputIterator out にコピーする. fmtの書式指定子やエスケープシーケンスは, それが表す文字か,参照する *this の文字シーケンスに置き換えられる. flags で指定されたビットマスクは Copies the character sequence [fmt.begin(), fmt.end()) to 書式指定子やエスケープシーケンスがどのように扱われるかを決定する. 既定では ECMA-262, ECMAScript Language Specification, Chapter 15 part 5.4.11 String.prototype.replace で使われる書式である.

Returns: out.


string_type format(const string_type& fmt,
match_flag_type flags = format_default);

Effects: 文字列 fmt を OutputIterator out にコピーする. fmtの書式指定子やエスケープシーケンスは, それが表す文字か,参照する *this の文字シーケンスに置き換えられる. flags で指定されたビットマスクは Copies the character sequence [fmt.begin(), fmt.end()) to 書式指定子やエスケープシーケンスがどのように扱われるかを決定する. 既定では ECMA-262, ECMAScript Language Specification, Chapter 15 part 5.4.11 String.prototype.replace で使われる書式である.

Allocator アクセス

allocator_type get_allocator()const;

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

Swap

void swap(match_results& that);

Effects: 二つのシーケンスの内容を入れ替える.

Postcondition: *thisthat の中にあった,マッチした子表現のシーケンスを保持する. that*this の中にあった,マッチした子表現のシーケンスを保持する.

Complexity: 定数時間

Captures

typedef typename value_type::capture_sequence_type capture_sequence_type;

標準ライブラリのシーケンスの要求(21.1.1 including the optional Table 68 operations)を満たす実装依存型を定義する. その value_type はsub_match<BidirectionalIterator> である. この型は今はたまたまstd::vector<sub_match<BidirectionalIterator> > であるが, それに依存するべきではない.

const capture_sequence_type& captures(std::size_t i)const; 

Effects: 子表現 iに対して得られた全てのキャプチャを含むシーケンスを返す.

Returns: (*this)[i].captures();

Preconditions: このメンバ関数が定義され,役立つ情報を返すためには, BOOST_REGEX_MATCH_EXTRA を定義してライブラリをビルドしていなければならず,また, フラグmatch_extraをBoost.regexのマッチ関数 (regex_match, regex_search, regex_iterator , regex_token_iterator) に渡さなければならない.

Rationale: この特徴が持つの結果を使えるようになる:

match_results 非メンバ

template <class BidirectionalIterator, class Allocator>
bool operator == (const match_results<BidirectionalIterator, Allocator>& m1,
const match_results<BidirectionalIterator, Allocator>& m2);

Effects: 二つのシーケンスが等しいか比較する.

template <class BidirectionalIterator, class Allocator>
bool operator != (const match_results<BidirectionalIterator, Allocator>& m1,
const match_results<BidirectionalIterator, Allocator>& m2);

Effects: 二つのシーケンスが等しくないか比較する.

template <class charT, class traits, class BidirectionalIterator, class Allocator>
basic_ostream<charT, traits>&
operator << (basic_ostream<charT, traits>& os,
const match_results<BidirectionalIterator, Allocator>& m);

Effects: mの内容を,os << m.str();が呼び出されたのと同じように, ストリームosに書き出す.osを返す.

template <class BidirectionalIterator, class Allocator>
void swap(match_results<BidirectionalIterator, Allocator>& m1,
match_results<BidirectionalIterator, Allocator>& m2);

Effects: 二つのシーケンスの内容を入れ替える.


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)