测试正则表达式是否与整个目标字符串相匹配。
// (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
元素的特征类。Alloc2
正则表达式分配器类。IOtraits
字符串特征类。IOalloc
字符串分配器类。flags
匹配标志。first
要匹配的序列的开头。last
要匹配的序列的结尾。match
匹配结果。 对应于 Elem 类型:smatch 匹配 string、wsmatch 匹配 wstring、cmatch 匹配 char* 或 wcmatch 匹配 wchar_t*。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