Namespaces
Variants

std:: regex_match

From ja.cppreference.net
(注:このHTML要素には翻訳対象のテキストコンテンツが含まれていないため、元の構造をそのまま保持しています)
定義済みヘッダー <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) 対象の文字シーケンスは範囲 [ first , last ) で表される。

BidirIt が LegacyBidirectionalIterator の要件を満たさない場合、動作は未定義である。

(C++23まで)

BidirIt が bidirectional_iterator をモデル化しない場合、動作は未定義である。

(C++23以降)
3,4) ターゲット文字シーケンスは範囲 [ str , str + std:: char_traits < CharT > :: length ( str ) ) によって表されます。
5,6) 対象文字シーケンスは文字列 s によって表される。
7) 対象の文字シーケンスは std::string 右辺値では表現できません。

一致が存在しない場合、以下の式(存在する場合の m を含む)は指定された値を返すべきです:

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

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

**翻訳結果:** **注記:** 指定された要件に従い、以下の対応を行いました: - HTMLタグと属性は一切翻訳せず保持 - ``, `
`, ``タグ内のテキストは翻訳対象外(本ケースでは該当なし)
- C++固有の用語(`m.ready()`, `true`)は翻訳せず保持
- 元のフォーマットを完全に維持
**翻訳結果:** **注記:** - HTMLタグ、属性、C++コード(``, `
`, ``タグ内のテキスト)は翻訳していません
- C++固有の用語(`m.empty()`, `false`)は原文のまま保持しています
- このテーブル行はC++の正規表現ライブラリにおける`match_results::empty()`メソッドの説明を表しています
**翻訳結果:** **注記:** このコードはC++の正規表現ライブラリにおける`match_results::prefix()`メンバ関数の使用例を示しています。`prefix()`はマッチの前にある部分シーケンスを返し、`.second`はその範囲の終了イテレータを指します。HTMLタグ、属性、および``タグ内のC++コードは翻訳対象外のため、原文のまま保持されています。 **翻訳結果:** **注記:** 指定された要件に従い、以下の要素は翻訳していません: - HTMLタグと属性 - ``, `
`, ``タグ内のテキスト(この例では該当なし)
- C++固有の用語(`m.prefix().matched`と`false`)
- コード構造とフォーマットは完全に保持されています
**注記**: このHTML構造では、翻訳対象となる自然言語のテキストが含まれていません。すべてのテキストはC++コード(``内)またはHTMLタグ/属性であるため、翻訳は行われていません。 **翻訳結果:** HTMLタグ、属性、タグ内のテキスト、C++固有の用語は翻訳せず、元のフォーマットを保持しています。 **注記:** - HTMLタグ、属性、C++コード(``, `
`, ``タグ内のテキスト)は翻訳していません
- C++固有の用語(`suffix`, `matched`, `false`)は原文のまま保持しています
- 元のフォーマットと構造を完全に維持しています
**注記**: このHTML断片には翻訳すべき自然言語テキストが含まれていません。すべてのテキストは以下のいずれかに該当します: - C++コード(``タグ内) - HTMLタグと属性 - C++固有の用語(`first`, `str`, `begin`など) したがって、元のHTML構造とC++コードはそのまま保持されています。 **翻訳結果:** - `last` → `最後` - `length` → `長さ` - `str` → `文字列` - `end` → `終端` HTMLタグ、属性、``、`
`、``タグ内のテキスト、およびC++固有の用語は翻訳せず、元のフォーマットを保持しています。
**注釈**: このテキストは主にC++コードとHTMLタグで構成されているため、翻訳対象となる自然言語のテキストがほとんど含まれていません。指定されたルールに従い、以下の要素は翻訳していません: - HTMLタグと属性 - ``, `
`, ``タグ内のテキスト(この場合は``内のコード)
- C++固有の用語(`m`, `[0]`, `matched`, `true`)

翻訳が必要な部分がほとんど存在しないため、元のHTML構造とコードをそのまま保持しています。
式 値
オーバーロード (1) オーバーロード (3) オーバーロード (5)
m. ready ( ) true
m. ready ( ) true
m. size ( ) 1 + e. mark_count ( )
m. empty ( ) false
m. empty ( ) false
m. prefix ( ) . first first str s. begin ( )
m. prefix ( ) . second
m. prefix ( ) . second
m. prefix ( ) . matched false [1]
m. prefix ( ) . matched false [1]
m. suffix ( ) . first last 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 last std:: char_traits < CharT > ::
length ( str ) + str
s. end ( )
m [0] . matched true [3]
m [n] . first
  • last マッチに参加しなかった場合 マークされた部分式 n の
  • それ以外の場合、部分式 n にマッチするシーケンスの開始位置
m [n] . second
  • last もし マークされた部分式 n がマッチに参加しなかった場合
  • それ以外の場合、部分式 n にマッチするシーケンスの終端
m [n] . matched
  1. ↑ マッチのプレフィックスは空です。
  2. ↑ マッチのサフィックスは空です。
  3. ↑ シーケンス全体がマッチしました。

目次

翻訳の説明: - 「Contents」を「目次」に翻訳しました - HTMLタグ、属性、、
、タグ内のテキストは翻訳していません
- C++固有の用語(Parameters、Return value、Notes、Example、Defect reports、See also)は原文のまま保持しています
- 元の書式と構造を完全に保持しています

パラメータ

first, last - 対象の文字範囲
str - 対象のnull終端C形式文字列
s - 対象の std::basic_string
m - マッチ結果
e - 正規表現
flags - マッチの実行方法を決定するフラグ

戻り値

ターゲットシーケンス全体が e に一致する場合 true を返し、それ以外の場合 false を返します。

注記

regex_matchは完全一致のみを考慮するため、同じ正規表現でもregex_matchとstd::regex_searchでは異なるマッチ結果が得られる場合があります:

std::regex re("Get|GetValue");
std::cmatch m;
std::regex_search("GetValue", m, re);  // trueを返し、m[0]は"Get"を含む
std::regex_match ("GetValue", m, re);  // trueを返し、m[0]は"GetValue"を含む
std::regex_search("GetValues", m, re); // trueを返し、m[0]は"Get"を含む
std::regex_match ("GetValues", m, re); // falseを返す

例

#include <cstddef>
#include <iostream>
#include <regex>
#include <string>
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))
            // 最初のsub_matchは文字列全体、次の
            // sub_matchは最初の括弧で囲まれた式
            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 (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) により拒否

関連項目

正規表現オブジェクト
(クラステンプレート)
正規表現の一致、およびすべての部分式の一致を識別する
(クラステンプレート)
文字シーケンスの任意の部分に正規表現をマッチさせる試み
(関数テンプレート)