std::regex_search
ヘッダ <regex> で定義
|
||
template< class BidirIt, class Alloc, class CharT, class Traits > |
(1) | (C++11以上) |
template< class CharT, class Alloc, class Traits > bool regex_search( const CharT* str, |
(2) | (C++11以上) |
template< class STraits, class SAlloc, class Alloc, class CharT, class Traits > |
(3) | (C++11以上) |
template< class BidirIt, class CharT, class Traits > |
(4) | (C++11以上) |
template< class CharT, class Traits > bool regex_search( const CharT* str, |
(5) | (C++11以上) |
template< class STraits, class SAlloc, class CharT, class Traits > |
(6) | (C++11以上) |
template< class STraits, class SAlloc, class Alloc, class CharT, class Traits > |
(7) | (C++14以上) |
正規表現 e
と、ターゲット文字シーケンス内の何らかの部分シーケンスの間で、マッチが存在するかどうかを判定します。
[first,last)
を解析します。 マッチ結果は m
に返されます。str
の指すヌル終端文字列を解析します。 マッチ結果は m
に返されます。s
を解析します。 マッチ結果は m
に返されます。std::regex_match は正規表現がシーケンス全体にマッチする場合にのみ true を返すのに対して、 regex_search
は指定されたシーケンスの任意の部分シーケンスのマッチに成功します。
目次 |
[編集] 引数
first, last | - | ターゲット文字シーケンスを表す範囲 |
str | - | ターゲット文字シーケンスを表すNULL終端文字列を指すポインタ |
s | - | ターゲット文字シーケンスを表す文字列 |
e | - | ターゲット文字シーケンスに適用されるべき std::regex |
m | - | マッチ結果 |
flags | - | 検索の動作に影響を与える std::regex_constants::match_flag_type |
型の要件 | ||
-BidirIt は LegacyBidirectionalIterator の要件を満たさなければなりません。
| ||
-Alloc は Allocator の要件を満たさなければなりません。
|
[編集] 戻り値
マッチが存在する場合は 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
[編集] 関連項目
(C++11) |
正規表現オブジェクト (クラステンプレート) |
(C++11) |
すべての部分表現のマッチを含む1つの正規表現マッチを識別します (クラステンプレート) |
(C++11) |
文字シーケンス全体への正規表現のマッチを試みます (関数テンプレート) |