|
|
Boost.RegexAlgorithm regex_search |
|
#include <boost/regex.hpp>
regex_search アルゴリズムは双方向イテレータのペアで指示された範囲から,与えられた正規表現を探し出す. アルゴリズムはマッチがその位置で開始し得るかどうかのチェックのみを行うことによって, 探索時間を減らすために様々な発見的方法を使っている.
template <class BidirectionalIterator, class Allocator, class charT, class traits, class Allocator2> bool regex_search(BidirectionalIterator first, BidirectionalIterator last, match_results<BidirectionalIterator, Allocator>& m, const basic_regex<charT, traits, Allocator2>& e, match_flag_type flags = match_default); template <class ST, class SA, class Allocator, class charT, class traits, class Allocator2> bool regex_search(const basic_string<charT, ST, SA>& s, match_results< typename basic_string<charT, ST,SA>::const_iterator, Allocator>& m, const basic_regex<charT, traits, Allocator2>& e, match_flag_type flags = match_default); template<class charT, class Allocator, class traits, class Allocator2> bool regex_search(const charT* str, match_results<const charT*, Allocator>& m, const basic_regex<charT, traits, Allocator2>& e, match_flag_type flags = match_default); template <class BidirectionalIterator, class Allocator, class charT, class traits> bool regex_search(BidirectionalIterator first, BidirectionalIterator last, const basic_regex<charT, traits, Allocator>& e, match_flag_type flags = match_default); template <class charT, class Allocator, class traits> bool regex_search(const charT* str, const basic_regex<charT, traits, Allocator>& e, match_flag_type flags = match_default); template<class ST, class SA, class Allocator, class charT, class traits> bool regex_search(const basic_string<charT, ST, SA>& s, const basic_regex<charT, traits, Allocator>& e, match_flag_type flags = match_default);
template <class BidirectionalIterator, class Allocator, class charT, class traits, class Allocator2> bool regex_search(BidirectionalIterator first, BidirectionalIterator last, match_results<BidirectionalIterator, Allocator>& m, const basic_regex<charT, traits, Allocator2>& e, match_flag_type flags = match_default);
Requires: BidirectionalIterator 型は Bidirectional Iterator (24.1.4) の要求を満たす.
Effects: [first,last) の中に正規表現 e にマッチする部分シーケンスがあるか決定する. flags は正規表現が文字シーケンスにどのようにマッチするかを制御する. マッチしたシーケンスがあれば ture なければ false を返す.
Throws:
N 文字の文字列に対する正規表現のマッチングの計算量が O(N2) を超える場合,
正規表現のマッチングの間にスタック空間を使い切ってしまった場合
(Boost.regex が再帰モードで構成されている時のみ),
またはマッチングで許容されたメモリ割り当てを消費しきってしまった場合
(Boost.regex が非再帰モードで構成されている時のみ),
std::runtime_error を投げる.
Postconditions: 関数が false を返すなら,パラメータ m は未定義. true を返すならパラメータ m は以下の表に示す状態になっている:
|
Element |
Value |
|
m.size() |
e.mark_count() |
|
m.empty() |
false |
|
m.prefix().first |
first |
|
m.prefix().last |
m[0].first |
|
m.prefix().matched |
m.prefix().first != m.prefix().second |
|
m.suffix().first |
m[0].second |
|
m.suffix().last |
last |
|
m.suffix().matched |
m.suffix().first != m.suffix().second |
|
m[0].first |
正規表現にマッチした文字シーケンスの始点 |
|
m[0].second |
正規表現にマッチした文字シーケンスの終点 |
|
m[0].matched |
全体マッチが発見されれば |
|
m[n].first |
全ての整数 n < m.size() で,マッチした部分表現 n のシーケンスの始点. 部分表現nがマッチしていなければ,last. |
|
m[n].second |
全ての整数 n < m.size() で,マッチした部分表現 n のシーケンスの終点. 部分表現nがマッチしていなければ,last. |
|
m[n].matched |
全ての整数 n < m.size() で,部分表現 n がマッチしたものであれば ture. そうでなければ false. |
template <class charT, class Allocator, class traits, class Allocator2> bool regex_search(const charT* str, match_results<const charT*, Allocator>& m, const basic_regex<charT, traits, Allocator2>& e, match_flag_type flags = match_default);
Effects: regex_search(str, str +
char_traits<charT>::length(str), m, e, flags)の結果を返す.
template <class ST, class SA, class Allocator, class charT, class traits, class Allocator2> bool regex_search(const basic_string<charT, ST, SA>& s, match_results<typename basic_string<charT, ST, SA>::const_iterator, Allocator>& m, const basic_regex<charT, traits, Allocator2>& e, match_flag_type flags = match_default);
Effects: regex_search(s.begin(), s.end(), m,
e, flags)の結果を返す.
template <class iterator, class Allocator, class charT, class traits> bool regex_search(iterator first, iterator last, const basic_regex<charT, traits, Allocator>& e, match_flag_type flags = match_default);
Effects:
match_results<BidirectionalIterator> what
がコンストラクとされた「かのように」振る舞い,
regex_search(first, last, what, e, flags)の結果を返す.
template <class charT, class Allocator, class traits> bool regex_search(const charT* str const basic_regex<charT, traits, Allocator>& e, match_flag_type flags = match_default);
Effects: regex_search(str, str +
char_traits<charT>::length(str), e, flags)の結果を返す.
template <class ST, class SA, class Allocator, class charT, class traits> bool regex_search(const basic_string<charT, ST, SA>& s, const basic_regex<charT, traits, Allocator>& e, match_flag_type flags = match_default);
Effects: regex_search(s.begin(), s.end(), e,
flags)の結果を返す.
次の例は, 文字列形式でファイルの内容を受け取り,ファイル中の全ての C++ クラス宣言を探索する. コードは std::string の実装方法に関わらず動く. 例えば,非連続的なストレージを使っている SGI rope クラスで動くように変更するのは簡単である.
#include <string>
#include <map>
#include <boost/regex.hpp>
// purpose:
// takes the contents of a file in the form of a string
// and searches for all the C++ class definitions, storing
// their locations in a map of strings/int's
typedef std::map<std::string, int, std::less<std::string> > map_type;
boost::regex expression("^(template[[:space:]]*<[^;:{]+>[[:space:]]*)?(class|struct)[[:space:]]*(\\<\\w+\\>([[:blank:]]*\\([^)]*\\))?[[:space:]]*)*(\\<\\w*\\>)[[:space:]]*(<[^;:{]+>[[:space:]]*)?(\\{|:[^;\\{()]*\\{)");
void IndexClasses(map_type& m, const std::string& file)
{
std::string::const_iterator start, end;
start = file.begin();
end = file.end();
boost::match_results<std::string::const_iterator> what;
boost::match_flag_type flags = boost::match_default;
while(regex_search(start, end, what, expression, flags))
{
// what[0] contains the whole string
// what[5] contains the class name.
// what[6] contains the template specialisation if any.
// add class name and position to map:
m[std::string(what[5].first, what[5].second) + std::string(what[6].first, what[6].second)] =
what[5].first - file.begin();
// update search position:
start = what[0].second;
// update flags:
flags |= boost::match_prev_avail;
flags |= boost::match_not_bob;
}
}
Revised 04 Feb 2004
ゥ Copyright John Maddock 1998- 2004
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)