Answers for "c++ regex match"

C++
0

cpp regex match

std::regex re("Get|GetValue");
std::cmatch m;
std::regex_search("GetValue", m, re);  // returns true, and m[0] contains "Get"
std::regex_match ("GetValue", m, re);  // returns true, and m[0] contains "GetValue"
std::regex_search("GetValues", m, re); // returns true, and m[0] contains "Get"
std::regex_match ("GetValues", m, re); // returns false
Posted by: Guest on January-11-2021
0

regex_match Function in C++

#include <iostream>
#include <string>
#include <regex>
using namespace std;
 
int main () {
 
   if (regex_match ("softwareTesting", regex("(soft)(.*)") ))
      cout << "string:literal => matchedn";
 
   const char mystr[] = "SoftwareTestingHelp";
   string str ("software");
   regex str_expr ("(soft)(.*)");
 
   if (regex_match (str,str_expr))
      cout << "string:object => matchedn";
 
   if ( regex_match ( str.begin(), str.end(), str_expr ) )
      cout << "string:range(begin-end)=> matchedn";
 
   cmatch cm;
   regex_match (mystr,cm,str_expr);
    
   smatch sm;
   regex_match (str,sm,str_expr);
    
   regex_match ( str.cbegin(), str.cend(), sm, str_expr);
   cout << "String:range, size:" << sm.size() << " matchesn";
 
   
   regex_match ( mystr, cm, str_expr, regex_constants::match_default );
 
   cout << "the matches are: ";
   for (unsigned i=0; i<sm.size(); ++i) {
      cout << "[" << sm[i] << "] ";
   }
 
   cout << endl;
 
   return 0;
}
Posted by: Guest on August-19-2021

Browse Popular Code Answers by Language