Boost.Regex(日本語訳)
latest

目次

  • 構成
  • ライブラリのビルドとインストール
  • 導入と概要
  • Unicode と Boost.Regex
  • マーク済み部分式と捕捉の理解
  • 部分マッチ
  • 正規表現の構文
  • 検索・置換書式化文字列の構文
  • リファレンス
    • basic_regex
    • match_results
    • sub_match
    • regex_match
    • regex_search
      • 説明
      • 使用例
    • regex_replace
    • regex_iterator
    • regex_token_iterator
    • bad_expression
    • syntax_option_type
    • match_flag_type
    • error_type
    • regex_traits
    • 非標準文字列型に対するインターフェイス
    • POSIX 互換 C API
    • コンセプト
    • 非推奨のインターフェイス
    • 内部の詳細
  • 様々な背景に関する情報
Boost.Regex(日本語訳)
  • リファレンス
  • regex_search
  • Edit on GitHub

regex_search

#include <boost/regex.hpp>

アルゴリズム regex_search は、双方向イテレータの組で示される範囲から与えられた正規表現を検索する。このアルゴリズムは様々な発見的方法を用いて検索時間を短縮する。そのために、個々の位置からマッチが開始する可能性があるかチェックのみを行う。このアルゴリズムの定義は以下のとおりである。

template <class BidirectionalIterator,
         class Allocator, class charT, class traits>
bool regex_search(BidirectionalIterator first, BidirectionalIterator last,
                  match_results<BidirectionalIterator, Allocator>& m,
                  const basic_regex<charT, traits>& 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,
                  match_results<
                     typename basic_string<charT, ST,SA>::const_iterator,
                     Allocator>& m,
                  const basic_regex<charT, traits>& e,
                  match_flag_type flags = match_default);

template<class charT, class Allocator, class traits>
bool regex_search(const charT* str,
                  match_results<const charT*, Allocator>& m,
                  const basic_regex<charT, traits>& e,
                  match_flag_type flags = match_default);

template <class BidirectionalIterator, class charT, class traits>
bool regex_search(BidirectionalIterator first, BidirectionalIterator last,
                  const basic_regex<charT, traits>& e,
                  match_flag_type flags = match_default);

template <class charT, class traits>
bool regex_search(const charT* str,
                  const basic_regex<charT, traits>& e,
                  match_flag_type flags = match_default);

template<class ST, class SA, class charT, class traits>
bool regex_search(const basic_string<charT, ST, SA>& s,
                  const basic_regex<charT, traits>& e,
                  match_flag_type flags = match_default);

説明

template<class BidirectionalIterator, class Allocator, class charT, class traits>
bool regex_search(BidirectionalIterator first, BidirectionalIterator last, match_results<BidirectionalIterator, Allocator> &m, const basic_regex<charT, traits> &e, match_flag_type flags = match_default)
要件

型 BidirectionalIterator が双方向イテレータの要件(24.1.4)を満たす。

効果

[first, last) 中に正規表現 e にマッチする部分シーケンスが存在するか判定する。引数 flags は、式が文字シーケンスに対してどのようにマッチするかを制御するのに使用する。完全なマッチが存在する場合は真を、それ以外の場合は偽を返す。

例外

std::runtime_error -- 長さ N の文字列に対して式のマッチの計算量が O(N2) を超え始めた場合、正規表現のマッチ中にプログラムのスタック空間が枯渇した場合(Boost.Regex が再帰モードを使うように構成されているとき)、あるいはマッチオブジェクトが許可されているメモリ割り当てを消耗しきった場合(Boost.Regex が非再帰モードを使うように構成されているとき)。

事後条件

関数が偽を返した場合、引数 m の状態は未定義である。それ以外の場合は次の表のとおりである。

要素

値

m.size()

1 + e.mark_count()

m.empty()

false

m.prefix().first

first

m.prefix().second

m[0].first

m.prefix().matched

m.prefix().first != m.prefix.second

m.suffix().first

m[0].second

m.suffix().second

last

m.suffix().matched

m.suffix().first != m.suffix().second

m[0].first

正規表現にマッチした文字シーケンスの先頭

m[0].second

正規表現にマッチした文字シーケンスの終端

m[0].matched

完全マッチが見つかった場合は真、(match_partial フラグを設定した結果)部分マッチが見つかった場合は偽。

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 がマッチした場合は真、それ以外は偽。

template<class charT, class Allocator, class traits>
bool regex_search(const charT *str, match_results<const charT*, Allocator> &m, const basic_regex<charT, traits> &e, match_flag_type flags = match_default)
効果

regex_search(str, str + char_traits<charT>::length(str), m, e, flags) の結果を返す。

template<class ST, class SA, class Allocator, class charT, class traits>
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> &e, match_flag_type flags = match_default)
効果

regex_search(s.begin(), s.end(), m, e, flags) の結果を返す。

template<class iterator, class charT, class traits>
bool regex_search(iterator first, iterator last, const basic_regex<charT, traits> &e, match_flag_type flags = match_default)
効果

match_results<BidirectionalIterator> のインスタンス what を構築し、regex_search(first, last, what, e, flags) の結果を返す。

template<class charT, class traits>
bool regex_search(const charT *str, const basic_regex<charT, traits> &e, match_flag_type flags = match_default)
効果

regex_search(str, str + char_traits<charT>::length(str), e, flags) の結果を返す。

template<class ST, class SA, class charT, class traits>
bool regex_search(const basic_string<charT, ST, SA> &s, const basic_regex<charT, traits> &e, match_flag_type flags = match_default)
効果

regex_search(s.begin(), s.end(), e, flags) の結果を返す。

使用例

以下の例は、ファイルの内容を 1 つの文字列として読み取り、ファイル内の C++ クラス宣言をすべて検索する。このコードは std::string の実装方法に依存しない。例えば SGI の rope クラス(不連続メモリバッファが使われている)を使うように容易に修正できる。

#include <string>
#include <map>
#include <boost/regex.hpp>

// 目的:
// ファイルの内容を単一の文字列として受け取り、
// C++ クラス宣言をすべて検索し、それらの位置を
// 文字列対整数の辞書に保存する
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] には文字列全体が入り
      // what[5] にはクラス名が入る。
      // what[6] にはテンプレートの特殊化(あれば)が入り、
      // クラス名と位置を辞書に入れて対応させる:
      m[std::string(what[5].first, what[5].second)
            + std::string(what[6].first, what[6].second)]
         = what[5].first - file.begin();
      // 検索位置を更新する:
      start = what[0].second;
      // flags を更新する:
      flags |= boost::match_prev_avail;
      flags |= boost::match_not_bob;
   }
}
Previous Next

© Copyright 2020, exeal. Revision 8c3943c0.

Built with Sphinx using a theme provided by Read the Docs.
Read the Docs v: latest
Versions
latest
stable
Downloads
pdf
html
epub
On Read the Docs
Project Home
Builds