The
C++11 encapsulates its own regular expression, directly including the file, using the Regex class to declare an object initialization regular expression, regex expressionname ("Regular expression"), and regular expression specific syntax reference here; Regex_match () method to match, the match returns 1 successfully, and the 0;cmatch and Smatch classes hold the results of the char* and string types, which can be obtained by traversal;
regex_match example #include <iostream> #include <string> #include <regex> int main () {if std:: Regex_match ("Subject", Std::regex ("(sub) (. *)")) std::cout << "string literal matched\n";//String Matching const char
Cstr[] = "subject";
std::string s ("subject");
Std::regex E ("(sub) (. *)"); if (Std::regex_match (s,e)) std::cout << "string Object matched\n"//Character object matches if (Std::regex_match (), S.end (), E)) std::cout << "Range matched\n";//Select a specific location for matching std::cmatch cm;
Same as std::match_results<const char*> cm;
Std::regex_match (cstr,cm,e);
Std::cout << "string literal with" << cm.size () << "matches\n"; Std::smatch SM;
Same as std::match_results<string::const_iterator> 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" <&Lt
Sm.size () << "matches\n";
Using explicit Flags: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; }
Summary of small issues:
1. Some restrictions on the regular expression above, such as icase:case insensitive (ignore case) Regex expressionname ("Regular expression", std::regex::icase).
2. Regular expressions pay attention to the use of escape characters in C + + code, because of the transfer function of C + + code itself, requires two "\" operations to implement, such as the regular expression needs to match "\", the regular expression is "\", then I need "\" in my C + + code, the first "\" Escape description of the second "\" as "\", in the same way the third "\" Escape description fourth "\" is "\" and gets the regular expression "\".