名前空間
変種
操作

std::regex_match

From cppreference.com
< cpp‎ | regex
 
 
 
正規表現ライブラリ
クラス
(C++11)
アルゴリズム
regex_match
(C++11)
イテレータ
例外
Traits
定数
(C++11)
正規表現文法
 
ヘッダ <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 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 );
(4) (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 );
(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++11以降)

正規表現 e がターゲット文字列シーケンス全体にマッチするかどうかを判定します。詳細なマッチ結果は(存在する場合)m に格納されます。

1,2) ターゲット文字列シーケンスは範囲 [firstlast) で表されます。

BidirItLegacyBidirectionalIterator の要件を満たさない場合、動作は未定義です。

(C++23まで)

BidirItbidirectional_iterator をモデル化しない場合、動作は未定義です。

(C++23から)
3,4) ターゲット文字列シーケンスは範囲 [strstr + std::char_traits<CharT>::length(str)) で表されます。
5,6) ターゲット文字列シーケンスは文字列 s で表されます。
7) ターゲット文字列シーケンスは std::string の右辺値では表せません。

マッチが存在しない場合、m (存在する場合)に関する以下の式は、指定された値を返すべきです。

 式   値 
m.ready() true
m.size() 0
m.empty() true

マッチが存在する場合、(0m.size()) の範囲の任意の整数を n として、以下の式は、以下にリストされた各オーバーロードについて、指定された値を返すべきです。

      式      
           オーバーロード (1)                       オーバーロード (3)                       オーバーロード (5)           
m.ready() true
m.size() 1 + e.mark_count()
m.empty() false
m.prefix().first first str s.begin()
m.prefix().second
m.prefix().matched     false[1]
m.suffix().first 最後 std::char_traits<CharT>::
    length(str) + str
s.end()
m.suffix().second
m.suffix().matched     false[2]
m[0].first first str s.begin()
m[0].second 最後 std::char_traits<CharT>::
    length(str) + str
s.end()
m[0].matched     true[3]
m[n].first
  • マッチしたサブ式 n がマッチに参加しなかった場合、シーケンスの終端
  • それ以外の場合、マッチしたサブ式 n の開始位置
m[n].second
  • マッチしたサブ式 n がマッチに参加しなかった場合、シーケンスの終端
  • マッチしたサブ式 n の終端
m[n].matched
  • マッチしたサブ式 n がマッチに参加しなかった場合、false
  • それ以外の場合、true
  1. マッチプレフィックスは空です。
  2. マッチサフィックスは空です。
  3. シーケンス全体がマッチしました。

目次

[編集] パラメータ

first, last - 対象の文字範囲
str - 対象のヌル終端Cスタイルの文字列
s - ターゲット std::basic_string
m - マッチ結果
e - 正規表現
flags - 一致の実行方法を決定するために使用されるフラグ

[編集] 戻り値

ターゲットシーケンス全体が e にマッチした場合、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 <cstddef>
#include <iostream>
#include <regex>
#include <string>
 
int main()
{
    // Simple regular expression matching
    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';
 
    // Extraction of a sub-match
    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))
            // The first sub_match is the whole string; the next
            // sub_match is the first parenthesized expression.
            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';
            }
 
    // Extraction of several sub-matches
    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 (std::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

[編集] 不具合報告

以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。

DR 適用対象 公開された動作 正しい動作
LWG 2205 C++11 n は後条件でゼロになる可能性がある 正の値しか取れない
LWG 2273 C++11 部分的なマッチが考慮されるか不明確だった 完全なマッチのみを考慮する
LWG 2329 C++11 オーバーロード (5)basic_string の右辺値を受け入れた
これにより、ダングリングイテレータが発生する可能性があった
削除されたオーバーロード (7) によって拒否された

[編集] 関連項目

正規表現オブジェクト
(クラステンプレート) [編集]
すべての部分式マッチを含む、1つの正規表現マッチを識別する
(クラステンプレート) [編集]
正規表現を文字シーケンスの任意の部分にマッチさせようと試みる
(関数テンプレート) [編集]
English 日本語 中文(简体) 中文(繁體)