match_results size() in C++ STL


In this article we will be discussing the working, syntax and examples of match_results::size() function 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::size()?

match_results::size() function is an inbuilt function in C++ STL, which is defined in <regex> header file. size() is used to get the number of matches of the match_results object associated with it. This function returns a size_type value that is the number of matches and sub-matches are there in the object associated with the function.

Syntax

smatch_name.size();

Parameters

This function accepts no parameters.

Return value

This function returns size_type size or the number of the matches and sub matches of the match_results object.

Example

Input: string str = "Tutorials Point";
   regex R("(Tutorials)(.*)");
   smatch Mat;
   regex_match(str, Mat, R);
   Mat.size();
Output: 3

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main() {
   string str = "Tutorials Point";
   regex R("(Tutorials)(.*)");
   smatch Mat;
   regex_match(str, Mat, R);
   cout<<"Size is: " << Mat.size() << endl;
   return 0;
}

Output

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

Size is: 3

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main() {
   string str = "Tutorials Point Tutorials";
   regex R("(Tutorials)(.*)");
   smatch Mat;
   regex_match(str, Mat, R);
   for (int i = 0; i < Mat.size(); i++) {
      cout <<"length of "<<Mat.length(i)<< endl;
   }
   return 0;
}

Output

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

length of 25
length of 9
length of 16

Updated on: 23-Mar-2020

61 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements