std:: regex_match
|
定義済みヘッダー
<regex>
|
||
|
template
<
class
BidirIt,
class
Alloc,
class
CharT,
class
Traits
>
bool
regex_match
(
BidirIt first, BidirIt last,
|
(1) | (C++11以降) |
|
template
<
class
BidirIt,
class
CharT,
class
Traits
>
bool
regex_match
(
BidirIt first, BidirIt last,
|
(2) | (C++11以降) |
|
template
<
class
CharT,
class
Alloc,
class
Traits
>
bool
regex_match
(
const
CharT
*
str,
|
(3) | (C++11以降) |
|
template
<
class
CharT,
class
Traits
>
bool
regex_match
(
const
CharT
*
str,
const
std::
basic_regex
<
CharT, Traits
>
&
e,
|
(4) | (C++11以降) |
|
template
<
class
STraits,
class
SAlloc,
class
Alloc,
class
CharT,
class
Traits
>
|
(5) | (C++11以降) |
|
template
<
class
STraits,
class
SAlloc,
class
CharT,
class
Traits
>
bool
regex_match
(
const
std::
basic_string
<
CharT, STraits, SAlloc
>
&
s,
|
(6) | (C++11以降) |
|
template
<
class
STraits,
class
SAlloc,
class
Alloc,
class
CharT,
class
Traits
>
|
(7) | (C++11以降) |
正規表現 e がターゲット文字シーケンス全体に一致するかどうかを判定します。詳細なマッチ結果は m に格納されます(存在する場合)。
[
first
,
last
)
で表される。
|
|
(C++23まで) |
|
|
(C++23以降) |
[
str
,
str
+
std::
char_traits
<
CharT
>
::
length
(
str
)
)
によって表されます。
一致が存在しない場合、以下の式(存在する場合の m を含む)は指定された値を返すべきです:
| 式 | 値 |
|---|---|
| m. ready ( ) | true |
| m. size ( ) | 0 |
| m. empty ( ) | true |
マッチが存在する場合、任意の整数を
(
0
,
m.
size
(
)
)
として
n
とすると、
m
を含む以下の式は、以下にリストされた各オーバーロードに対して指定された値を返すべきです:
| 式 | 値 | ||
|---|---|---|---|
| オーバーロード (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 |
|
||
| m [n] . second |
|
||
| m [n] . matched |
|
||
目次 |
、、
パラメータ
| 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) により拒否 |
関連項目
|
(C++11)
|
正規表現オブジェクト
(クラステンプレート) |
|
(C++11)
|
正規表現の一致、およびすべての部分式の一致を識別する
(クラステンプレート) |
|
(C++11)
|
文字シーケンスの任意の部分に正規表現をマッチさせる試み
(関数テンプレート) |