regex_match
描述 (Description)
它返回目标序列是否与正则表达式rgx匹配。 目标序列是s或第一个和最后一个之间的字符序列,具体取决于使用的版本。
声明 (Declaration)
以下是std :: regex_match的声明。
template <class charT, class traits>
bool regex_match (const charT* s, const basic_regex<charT,traits>& rgx,
regex_constants::match_flag_type flags = regex_constants::match_default);
C++11
template <class charT, class traits>
bool regex_match (const charT* s, const basic_regex<charT,traits>& rgx,
regex_constants::match_flag_type flags = regex_constants::match_default);
C++14
template <class charT, class traits>
bool regex_match (const charT* s, const basic_regex<charT,traits>& rgx,
regex_constants::match_flag_type flags = regex_constants::match_default);
参数 (Parameters)
s - 它是一个包含目标序列的字符串。
rgx - 它是一个匹配的basic_regex对象。
flags - 用于控制rgx的匹配方式。
m - 它是match_results类型的对象。
返回值 (Return Value)
如果rgx与目标序列匹配,则返回true。 否则是假的。
异常 (Exceptions)
No-noexcept - 这个成员函数永远不会抛出异常。
例子 (Example)
在下面的示例中为std :: regex_match。
#include <iostream>
#include <string>
#include <regex>
int main () {
if (std::regex_match ("subject", std::regex("(sub)(.*)") ))
std::cout << "string literal matched\n";
const char cstr[] = "subject";
std::string s ("subject");
std::regex e ("(sub)(.*)");
if (std::regex_match (s,e))
std::cout << "string object matched\n";
if ( std::regex_match ( s.begin(), s.end(), e ) )
std::cout << "range matched\n";
std::cmatch cm;
std::regex_match (cstr,cm,e);
std::cout << "string literal with " << cm.size() << " matches\n";
std::smatch sm;
std::regex_match (s,sm,e);
std::cout << "string object with " << sm.size() << " matches\n";
std::regex_match ( s.cbegin(), s.cend(), sm, e);
std::cout << "range with " << sm.size() << " matches\n";
std::regex_match ( cstr, cm, e, std::regex_constants::match_default );
std::cout << "the matches were: ";
for (unsigned i=0; i<sm.size(); ++i) {
std::cout << "[" << sm[i] << "] ";
}
std::cout << std::endl;
return 0;
}
输出应该是这样的 -
string literal matched
string object matched
range matched
string literal with 3 matches
string object with 3 matches
range with 3 matches
the matches were: [subject] [sub] [ject]