名前空間
変種
操作

std::regex_match

提供: cppreference.com
< cpp‎ | regex
ヘッダ <regex> で定義
template< class BidirIt,

          class Alloc, class CharT, class Traits >
bool regex_match( 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 BidirIt,

          class CharT, class Traits >
bool regex_match( BidirIt first, BidirIt last,
                  const std::basic_regex<CharT,Traits>& e,
                  std::regex_constants::match_flag_type flags =

                      std::regex_constants::match_default );
(2) (C++11以上)
template< class CharT, class Alloc, class Traits >

bool regex_match( 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 );
(3) (C++11以上)
template< class STraits, class SAlloc,

          class Alloc, class CharT, class Traits >
bool regex_match( 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 );
(4) (C++11以上)
template< class CharT, class Traits >

bool regex_match( 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_match( 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_match( 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以上)

正規表現 estd::string、 C の文字列、またはイテレータの組として指定されるターゲット文字シーケンス全体にマッチするかどうかを判定します。

1) flags の効果を考慮して、正規表現 e とターゲット文字シーケンス [first,last) 全体の間のマッチが存在するかどうか判定します。 マッチの存在を判定するとき、文字シーケンス全体にマッチするマッチの可能性のみが考慮されます。 マッチの結果は m に返されます。
2) 上の (1) のように動作しますが、マッチ結果は省略されます。
3) std::regex_match(str, str + std::char_traits<charT>::length(str), m, e, flags) を返します。
4) std::regex_match(s.begin(), s.end(), m, e, flags) を返します。
5) std::regex_match(str, str + std::char_traits<charT>::length(str), e, flags) を返します。
6) std::regex_match(s.begin(), s.end(), e, flags) を返します。
7) オーバーロード 4 が一時的な文字列を受理することを防ぎます。 さもなければ、直ちに無効になる文字列イテレータを match_results m が保持してしまいます。

std::regex_search が部分シーケンスのマッチに成功するのに対して、 regex_match は正規表現を文字シーケンス全体のマッチにのみ成功することに注意してください。

目次

[編集] 引数

first, last - イテレータとして指定された、正規表現を適用するターゲット文字範囲
m - マッチ結果
str - C スタイルのヌル終端文字列として指定された、ターゲット文字列
s - std::basic_string として指定された、ターゲット文字列
e - 正規表現
flags - マッチがどのように行われるかを決定するために使用するフラグ
型の要件
-
BidirItLegacyBidirectionalIterator の要件を満たさなければなりません。

[編集] 戻り値

マッチが存在する場合は 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 first
m.prefix().matched false (マッチの接頭辞は空です)
m.suffix().first last
m.suffix().second last
m.suffix().matched false (マッチの接尾辞は空です)
m[0].first first
m[0].second last
m[0].matched true (シーケンス全体にマッチします)
m[n].first n 番目のマーク付き部分表現にマッチしたシーケンスの開始位置、または部分表現がマッチしなかった場合は last
m[n].second n 番目のマーク付き部分表現にマッチしたシーケンスの終了位置、または部分表現がマッチしなかった場合は last
m[n].matched n 番目の部分表現がマッチした場合は true、そうでなければ false

[編集] ノート

regex_match はマッチ全体のみを考慮するため、同じ正規表現が regex_matchstd::regex_search で異なる結果を出す場合があります。

std::regex re("Get|GetValue");
std::cmatch m;
std::regex_search("GetValue", m, re);  // returns true, and m[0] contains "Get"
std::regex_match ("GetValue", m, re);  // returns true, and m[0] contains "GetValue"
std::regex_search("GetValues", m, re); // returns true, and m[0] contains "Get"
std::regex_match ("GetValues", m, re); // returns false

[編集]

#include <iostream>
#include <string>
#include <regex>
 
int main()
{
    // 単純な正規表現のマッチ。
    const std::string fnames[] = {"foo.txt", "bar.txt", "baz.dat", "zoidberg"};
    const std::regex txt_regex("[a-z]+\\.txt");
 
    for (const auto &fname : fnames) {
        std::cout << fname << ": " << std::regex_match(fname, txt_regex) << '\n';
    }   
 
    // 部分マッチの抽出。
    const std::regex base_regex("([a-z]+)\\.txt");
    std::smatch base_match;
 
    for (const auto &fname : fnames) {
        if (std::regex_match(fname, base_match, base_regex)) {
            // 1つめの sub_match は文字列全体です。
            // 2つめの sub_match は1つめの括弧で囲まれた表現です。
            if (base_match.size() == 2) {
                std::ssub_match base_sub_match = base_match[1];
                std::string base = base_sub_match.str();
                std::cout << fname << " has a base of " << base << '\n';
            }
        }
    }
 
    // 複数の部分マッチの抽出。
    const std::regex pieces_regex("([a-z]+)\\.([a-z]+)");
    std::smatch pieces_match;
 
    for (const auto &fname : fnames) {
        if (std::regex_match(fname, pieces_match, pieces_regex)) {
            std::cout << fname << '\n';
            for (size_t i = 0; i < pieces_match.size(); ++i) {
                std::ssub_match sub_match = pieces_match[i];
                std::string piece = sub_match.str();
                std::cout << "  submatch " << i << ": " << piece << '\n';
            }   
        }   
    }   
}

出力:

foo.txt: 1
bar.txt: 1
baz.dat: 0
zoidberg: 0
foo.txt has a base of foo
bar.txt has a base of bar
foo.txt
  submatch 0: foo.txt
  submatch 1: foo
  submatch 2: txt
bar.txt
  submatch 0: bar.txt
  submatch 1: bar
  submatch 2: txt
baz.dat
  submatch 0: baz.dat
  submatch 1: baz
  submatch 2: dat

[編集] 関連項目

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