regex_search 関数

更新 : 2007 年 11 月

正規表現の一致を検索します。

template<class BidIt, class Alloc, class Elem, class RXtraits, class Alloc2>
    bool regex_search(BidIt first, Bidit last,
        match_results<BidIt, Alloc>& match,
        const basic_regex<Elem, RXtraits, Alloc2>& re,
        match_flag_type flags = match_default);
template<class BidIt, class Elem, class RXtraits, class Alloc2>
    bool regex_search(BidIt first, Bidit last,
        const basic_regex<Elem, RXtraits, Alloc2>& re,
        match_flag_type flags = match_default);
template<class Elem, class Alloc, class RXtraits, class Alloc2>
    bool regex_search(const Elem* ptr,
        match_results<const Elem*, Alloc>& match,
        const basic_regex<Elem, RXtraits, Alloc2>& re,
        match_flag_type flags = match_default);
template<class Elem, class RXtraits, class Alloc2>
    bool regex_search(const Elem* ptr,
        const basic_regex<Elem, RXtraits, Alloc2>& re,
        match_flag_type flags = match_default);
template<class IOtraits, class IOalloc, class Alloc, class Elem, class RXtraits, class Alloc2>
    bool regex_search(const basic_string<Elem, IOtraits, IOalloc>& str,
        match_results<typename basic_string<Elem, IOtraits, IOalloc>::const_iterator, Alloc>& match,
        const basic_regex<Elem, RXtraits, Alloc2>& re,
        match_flag_type flags = match_default);
template<class IOtraits, class IOalloc, class Elem, class RXtraits, class Alloc2>
    bool regex_search(const basic_string<Elem, IOtraits, IOalloc>& str,
        const basic_regex<Elem, RXtraits, Alloc2>& re,
        match_flag_type flags = match_default);

パラメータ

  • BidIt
    サブマッチの反復子の型。

  • Alloc
    一致した結果のアロケータ クラス。

  • Elem
    一致させる要素の型。

  • RXtraits
    要素の特徴 (traits) クラス。

  • Alloc2
    正規表現のアロケータ クラス。

  • IOtraits
    文字列の特徴クラス。

  • IOalloc
    文字列のアロケータ クラス。

  • flags
    一致のフラグ。

  • first
    一致させるシーケンスの最初。

  • last
    一致させるシーケンスの最後。

  • match
    一致した結果。

  • ptr
    一致させるシーケンスの先頭を指すポインタ。

  • re
    一致させる正規表現。

  • str
    一致させる文字列。

解説

各テンプレート関数は、対応するオペランド シーケンスの正規表現の引数 re の検索に成功した場合にのみ、true を返します。match_results オブジェクトを受け取る関数は、検索に成功したかどうかの結果と、成功した場合は、キャプチャされた正規表現の各種キャプチャ グループが反映されるようにそのメンバを設定します。

使用例

 

// std_tr1__regex__regex_search.cpp 
// compile with: /EHsc 
#include <regex> 
#include <iostream> 
 
int main() 
    { 
    const char *first = "abcd"; 
    const char *last = first + strlen(first); 
    std::tr1::cmatch mr; 
    std::tr1::regex rx("abc"); 
    std::tr1::regex_constants::match_flag_type fl = 
        std::tr1::regex_constants::match_default; 
 
    std::cout << "search(f, f+1, \"abc\") == " << std::boolalpha 
        << regex_search(first, first + 1, rx, fl) << std::endl; 
 
    std::cout << "search(f, l, \"abc\") == " << std::boolalpha 
        << regex_search(first, last, mr, rx) << std::endl; 
    std::cout << "  matched: \"" << mr.str() << "\"" << std::endl; 
 
    std::cout << "search(\"a\", \"abc\") == " << std::boolalpha 
        << regex_search("a", rx) << std::endl; 
 
    std::cout << "search(\"xabcd\", \"abc\") == " << std::boolalpha 
        << regex_search("xabcd", mr, rx) << std::endl; 
    std::cout << "  matched: \"" << mr.str() << "\"" << std::endl; 
 
    std::cout << "search(string, \"abc\") == " << std::boolalpha 
        << regex_search(std::string("a"), rx) << std::endl; 
 
    std::string str("abcabc"); 
    std::tr1::match_results<std::string::const_iterator> mr2; 
    std::cout << "search(string, \"abc\") == " << std::boolalpha 
        << regex_search(str, mr2, rx) << std::endl; 
    std::cout << "  matched: \"" << mr2.str() << "\"" << std::endl; 
 
    return (0); 
    } 
 
search(f, f+1, "abc") == false
search(f, l, "abc") == true
  matched: "abc"
search("a", "abc") == false
search("xabcd", "abc") == true
  matched: "abc"
search(string, "abc") == false
search(string, "abc") == true
  matched: "abc"

必要条件

ヘッダー : <regex>

名前空間 : std::tr1

参照

参照

<regex>

regex_match 関数

regex_replace 関数