regex_match-Funktion
Testet, ob ein regulärer Ausdruck der gesamten Zielzeichenfolge entspricht.
// (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);
Parameter
BidIt
Der Itertatortyp für Teilübereinstimmungen. In allgemeinen Fällen ist dies einer der folgenden: string::const_iterator, wstring::const_iterator, const char* oder const wchar_t*.Alloc
Die Zuweisungsklasse des Übereinstimmungsergebnisses.Elem
Der zu entsprechende Elementtyp. In allgemeinen Fällen ist dies string, wstring, char* oder wchar_t*.RXtraits
Merkmalklasse für Elemente.Alloc2
Die Zuweisungsklasse des regulären Ausdrucks.IOtraits
Die Trait-Klasse der Zeichenfolge.IOalloc
Die Zuweisungsklasse der Zeichenfolge.flags
Flags für Übereinstimmungen.first
Anfang der Sequenz, die übereinstimmen soll.last
Ende der Sequenz, die übereinstimmen soll.match
Die Übereinstimmungsergebnisse. Entspricht dem Elementtyp: smatch für string, wsmatch für wstring, cmatch für char* oder wcmatch für wchar_t*.ptr
Zeiger auf den Anfang der Sequenz, die übereinstimmen soll. Wenn ptr char* ist, verwenden Sie cmatch und regex. Wenn ptr wchar_t* ist, verwenden Sie wcmatch und wregex.re
Der reguläre Ausdruck, der übereinstimmen soll. Geben Sie regex für string und char* oder wregex für wstring und wchar_t* ein.str
Zeichenfolge, die übereinstimmen soll. Entspricht dem Elementtyp.
Hinweise
Jede Vorlagenfunktion gibt nur dann "true" zurück, wenn die gesamte Operandensequenz str exakt mit dem Argument für den regulären Ausdruck re übereinstimmt. Verwenden Sie regex_search, um nach einer Übereinstimmung einer untergeordneten Zeichenfolge in einer Zielsequenz zu suchen, und regex_iterator, um mehrere Übereinstimmungen zu finden.
Die Funktionen, die ein match_results-Objekt übernehmen, legen dessen Member fest, um wiederzugeben, ob die Suche nach der Übereinstimmung erfolgreich war, und wenn ja, was die verschiedenen Erfassungsgruppen im regulären Ausdruck erfasst haben.
(1):
Beispiel
// 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;
}
Anforderungen
Header: <regex>
Namespace: std