C + + implementation regular expression matching __ regular expression

Source: Internet
Author: User

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 "\".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.