名前空間
変種

std::regex_search

提供: cppreference.com
<tbody> </tbody>
ヘッダ <regex> で定義
template< class BidirIt, class Alloc, class CharT, class Traits > bool regex_search( BidirIt first, BidirIt last, std::match_results<BidirIt,Alloc>& m, const std::basic_regex<CharT,Traits>& e, std::regex_constants::match_flag_type flags = std::regex_constants::match_default );
(1) (C++11以上)
template< class CharT, class Alloc, class Traits > bool regex_search( const CharT* str, std::match_results<const CharT*,Alloc>& m, const std::basic_regex<CharT,Traits>& e, std::regex_constants::match_flag_type flags = std::regex_constants::match_default );
(2) (C++11以上)
template< class STraits, class SAlloc, class Alloc, class CharT, class Traits > bool regex_search( const std::basic_string<CharT,STraits,SAlloc>& s, std::match_results< typename std::basic_string<CharT,STraits,SAlloc>::const_iterator, Alloc >& m, const std::basic_regex<CharT, Traits>& e, std::regex_constants::match_flag_type flags = std::regex_constants::match_default );
(3) (C++11以上)
template< class BidirIt, class CharT, class Traits > bool regex_search( BidirIt first, BidirIt last, const std::basic_regex<CharT,Traits>& e, std::regex_constants::match_flag_type flags = std::regex_constants::match_default );
(4) (C++11以上)
template< class CharT, class Traits > bool regex_search( const CharT* str, const std::basic_regex<CharT,Traits>& e, std::regex_constants::match_flag_type flags = std::regex_constants::match_default );
(5) (C++11以上)
template< class STraits, class SAlloc, class CharT, class Traits > bool regex_search( const std::basic_string<CharT,STraits,SAlloc>& s, const std::basic_regex<CharT,Traits>& e, std::regex_constants::match_flag_type flags = std::regex_constants::match_default );
(6) (C++11以上)
template< class STraits, class SAlloc, class Alloc, class CharT, class Traits > bool regex_search( const std::basic_string<CharT,STraits,SAlloc>&&, std::match_results< typename std::basic_string<CharT,STraits,SAlloc>::const_iterator, Alloc >&, const std::basic_regex<CharT, Traits>&, std::regex_constants::match_flag_type flags = std::regex_constants::match_default ) = delete;
(7) (C++14以上)

正規表現 e と、ターゲット文字シーケンス内の何らかの部分シーケンスの間で、マッチが存在するかどうかを判定します。

1) 汎用範囲 [first,last) を解析します。 マッチ結果は m に返されます。
2) str の指すヌル終端文字列を解析します。 マッチ結果は m に返されます。
3) 文字列 s を解析します。 マッチ結果は m に返されます。
4-6) (1-3) と同等ですが、マッチ結果は省略されます。
7) オーバーロード 3 が一時的な文字列を受理することを防ぎます。 さもなければ、直ちに無効になる文字列イテレータを match_results m が保持してしまいます。

std::regex_match は正規表現がシーケンス全体にマッチする場合にのみ true を返すのに対して、 regex_search は指定されたシーケンスの任意の部分シーケンスのマッチに成功します。

引数

first, last - ターゲット文字シーケンスを表す範囲
str - ターゲット文字シーケンスを表すNULL終端文字列を指すポインタ
s - ターゲット文字シーケンスを表す文字列
e - ターゲット文字シーケンスに適用されるべき std::regex
m - マッチ結果
flags - 検索の動作に影響を与える std::regex_constants::match_flag_type
型の要件
-
BidirItLegacyBidirectionalIterator の要件を満たさなければなりません。
-
AllocAllocator の要件を満たさなければなりません。

戻り値

マッチが存在する場合は true、そうでなければ false を返します。 いずれの場合でも、オブジェクト m は以下のように更新されます。

マッチが存在しない場合、

m.ready() == true
m.empty() == true
m.size() == 0

マッチが存在する場合、

m.ready() true
m.empty() false
m.size() マーク付き部分表現の数プラス1、つまり 1+e.mark_count()
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 true
m[n].first n 番目のマーク付き部分表現にマッチしたシーケンスの開始位置、または部分表現がマッチしなかった場合は last
m[n].second n 番目のマーク付き部分表現にマッチしたシーケンスの終了位置、または部分表現がマッチしなかった場合は last
m[n].matched n 番目の部分表現がマッチした場合は true、そうでなければ false

ノート

ターゲットシーケンス内のすべてのマッチを検査するためには、 std::regex_search をループで呼び出し、各回を前回の呼び出しの m[0].second から再開します。 std::regex_iterator は、この反復の簡単なインタフェースを提供します。

#include <iostream>
#include <string>
#include <regex>

int main()
{
    std::string lines[] = {"Roses are #ff0000",
                           "violets are #0000ff",
                           "all of my base are belong to you"};

    std::regex color_regex("#([a-f0-9]{2})"
                            "([a-f0-9]{2})"
                            "([a-f0-9]{2})");

    // simple match
    for (const auto &line : lines) {
        std::cout << line << ": " << std::boolalpha
                  << std::regex_search(line, color_regex) << '\n';
    }   
    std::cout << '\n';
 
    // show contents of marked subexpressions within each match
    std::smatch color_match;
    for (const auto& line : lines) {
        if(std::regex_search(line, color_match, color_regex)) {
            std::cout << "matches for '" << line << "'\n";
            std::cout << "Prefix: '" << color_match.prefix() << "'\n";
            for (size_t i = 0; i < color_match.size(); ++i) 
                std::cout << i << ": " << color_match[i] << '\n';
            std::cout << "Suffix: '" << color_match.suffix() << "\'\n\n";
        }
    }

    // repeated search (see also std::regex_iterator)
    std::string log(R"(
        Speed:	366
        Mass:	35
        Speed:	378
        Mass:	32
        Speed:	400
	Mass:	30)");
    std::regex r(R"(Speed:\t\d*)");
    std::smatch sm;
    while(regex_search(log, sm, r))
    {
        std::cout << sm.str() << '\n';
        log = sm.suffix();
    }

    // C-style string demo
    std::cmatch cm;
    if(std::regex_search("this is a test", cm, std::regex("test"))) 
        std::cout << "\nFound " << cm[0] << " at position " << cm.prefix().length();
}

出力:

Roses are #ff0000: true
violets are #0000ff: true
all of my base are belong to you: false

matches for 'Roses are #ff0000'
Prefix: 'Roses are '
0: #ff0000
1: ff
2: 00
3: 00
Suffix: ''

matches for 'violets are #0000ff'
Prefix: 'violets are '
0: #0000ff
1: 00
2: 00
3: ff
Suffix: ''

Speed:	366
Speed:	378
Speed:	400

Found test at position 10

関連項目

正規表現オブジェクト
(クラステンプレート) [edit]
すべての部分表現のマッチを含む1つの正規表現マッチを識別します
(クラステンプレート) [edit]
文字シーケンス全体への正規表現のマッチを試みます
(関数テンプレート) [edit]