regex_match 関数
正規表現がターゲット文字列全体と一致するかどうかをテストします。
// (1) template<class BidIt, class Alloc, class Elem, class RXtraits, class Alloc2> bool regex_match(BidIt first, Bidit last, match_results<BidIt, Alloc>& match, const basic_regex<Elem, RXtraits, Alloc2>& re, match_flag_type flags = match_default); // (2) template<class BidIt, class Elem, class RXtraits, class Alloc2> bool regex_match(BidIt first, Bidit last, const basic_regex<Elem, RXtraits, Alloc2>& re, match_flag_type flags = match_default); // (3) template<class Elem, class Alloc, class RXtraits, class Alloc2> bool regex_match(const Elem *ptr, match_results<const Elem*, Alloc>& match, const basic_regex<Elem, RXtraits, Alloc2>& re, match_flag_type flags = match_default); // (4) template<class Elem, class RXtraits, class Alloc2> bool regex_match(const Elem *ptr, const basic_regex<Elem, RXtraits, Alloc2>& re, match_flag_type flags = match_default); // (5) template<class IOtraits, class IOalloc, class Alloc, class Elem, class RXtraits, class Alloc2> bool regex_match(const basic_string<Elem, IOtraits, IOalloc>& str, match_results<typename basic_string<Elem, IOtraits, IOalloc>::const_iterator, Alloc>& match, const basic_regex<Elem, RXtraits, Alloc2>& re, match_flag_type flags = match_default); // (6) template<class IOtraits, class IOalloc, class Elem, class RXtraits, class Alloc2> bool regex_match(const basic_string<Elem, IOtraits, IOalloc>& str, const basic_regex<Elem, RXtraits, Alloc2>& re, match_flag_type flags = match_default);
パラメーター
BidIt
サブマッチ用の反復子の型。 一般的には、string::const_iterator、wstring::const_iterator、const char*、または const wchar_t* です。Alloc
一致結果のアロケーター クラス。Elem
一致させる要素の型。 一般的には、string、wstring、char*、または wchar_t* です。RXtraits
要素の特徴 (traits) クラス。Alloc2
正規表現のアロケーター クラス。IOtraits
文字列の特徴 (traits) クラス。IOalloc
文字列のアロケーター クラス。flags
一致のフラグ。first
一致させるシーケンスの先頭。last
一致させるシーケンスの末尾。match
一致結果。 Elem 型に対応します。string には smatch、wstring には wsmatch、char* には cmatch、wchar_t* には wcmatch が対応します。ptr
一致させるシーケンスの先頭を指すポインター。 ptr が char* の場合は、cmatch と regex を使用します。 ptr が wchar_t* の場合は、wcmatch と wregex を使用します。re
一致させる正規表現。 string および char* の場合は型 regexで、wstring および wchar_t* の場合は wregex です。str
一致させる文字列。 Elem の型に対応します。
解説
各テンプレート関数は、オペランド シーケンス str 全体が正規表現の引数 re に完全に一致している場合にのみ true を返します。 regex_search を使用して、ターゲット シーケンス内の部分文字列と一致させ、regex_iterator を使用して複数の一致を検出します。
match_results オブジェクトを受け取る関数は、一致が成功したかどうかを反映し、さらに成功した場合は正規表現内のさまざまなキャプチャ グループがキャプチャした内容を反映するようにそのメンバーを設定します。
(1):
使用例
// RegexTestBed.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <regex>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
// (1) with char*
// Note how const char* requires cmatch and regex
const char *first = "abc";
const char *last = first + strlen(first);
cmatch narrowMatch;
regex rx("a(b?)c");
bool found = regex_match(first, last, narrowMatch, rx);
// (1) with std::wstring
// Note how wstring requires wsmatch and wregex.
// Note use of const iterators cbegin() and cend().
wstring target(L"Hello");
wsmatch wideMatch;
wregex wrx(L"He(l+)o");
if (regex_match(target.cbegin(), target.cend(), wideMatch, wrx))
wcout << L"The matching text is:" << wideMatch.str() << endl;
// (2) with std::string
string target2("Drizzle");
regex rx2(R"(D\w+e)"); // no double backslashes with raw string literal
found = regex_match(target2.cbegin(), target2.cend(), rx2);
// (3) with wchar_t*
const wchar_t* target3 = L"2014-04-02";
wcmatch wideMatch2;
// LR"(...)" is a raw wide-string literal. Open and close parens
// are delimiters, not string elements.
wregex wrx2(LR"(\d{4}(-|/)\d{2}(-|/)\d{2})");
if (regex_match(target3, wideMatch2, wrx2))
{
wcout << L"Matching text: " << wideMatch2.str() << endl;
}
return 0;
}
必要条件
ヘッダー: <regex>
名前空間: std