match_results operator= in C++


In this article we will be discussing the working, syntax and examples of match_results operator ‘=’ in C++ STL.

What is a match_results in C++ STL?

std::match_results is a specialized container-like class which is used to hold the collection of character sequences which are matched. In this container class a regex match operation finds the matches of the target sequence.

What is match_results operator ‘=’

Match_results operator = is an equality operator which is used to assign the value to a match_results. Operator = is used to copy or move the elements from one match_results object to another.

Syntax

match_results1 = (match_results2);

Parameters

Another match_results object whose data we have to copy to a match_results object.

Return value

This returns nothing.

Example

Input: string str = "Tutorials Point";
   regex R("(Tutorials)(.*)");
   smatch Mat_1, Mat_2;
   regex_match(str, Mat_1, R);
   Mat_2 = Mat_1;
Output: MAT 2 =
   Tutorials Point
   Tutorials
   Point

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main() {
   string str = "Tutorials Point";
   regex R("(Tutorials)(.*)");
   smatch Mat_1, Mat_2;
   regex_match(str, Mat_1, R);
   Mat_2 = Mat_1;
   cout<<"String matches: " << endl;
   for (smatch::iterator i = Mat_2.begin(); i!= Mat_2.end(); i++) {
      cout << *i << endl;
   }
}

Output

If we run the above code it will generate the following output −

String matches:
Tutorials Point
Tutorials
Point

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main() {
   string str = "Tutorials Point";
   regex R_1("(Tutorials)(.*)");
   regex R_2("(Po)(int)(.*)");
   smatch Mat_1, Mat_2;
   regex_match(str, Mat_1, R_1);
   regex_match(str, Mat_2, R_2);
   smatch Mat;
   if (Mat_1.size() > Mat_2.size()) {
      Mat = Mat_1;
   } else {
      Mat = Mat_2;
   }
   cout<<"string matches " << endl;
   for (smatch::iterator i = Mat.begin(); i!= Mat.end(); i++) {
      cout << *i << endl;
   }
}

Output

If we run the above code it will generate the following output −

String matches:
Tutorials Point
Tutorials
Point

Updated on: 23-Mar-2020

64 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements