Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

match_results size() in C++ STL

  • Last Updated : 21 Jun, 2021

The match_results size() is a inbuilt function in C++ which returns the number of matches ans sub-matches in the match_result object.
Syntax: 
 

smatch_name.size()

Note: smatch_name is an object of match_results class.

Parameters: The function accept no parameters.
Return value: It returns the number of matches in the match_result object.
Note: First element always contains the whole regex match while the others contain the particular Capturing Group
Below programs illustrate the above function.
Program 1: 
 

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

CPP




// CPP program to illustrate
// match_results size() in C++
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    string s("Geeksforgeeks");
    regex re("(Geeks)(.*)");
 
    smatch match;
 
    // Function call to find match
    regex_match(s, match, re);
 
    cout << "match size is " << match.size() << endl;
 
    return 0;
}
Output: 
match size is 3

 

Program 2: 
 

CPP




// CPP program to illustrate
// match_results size() in C++
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    string s("Geeksforgeeks");
    regex re("(Geeks)(.*)");
 
    smatch match;
 
    // Function call to find matches
    regex_match(s, match, re);
 
    cout << "match size is " << match.size() << endl;
 
    for (int i = 0; i < match.size(); i++) {
        cout << "match " << i << " has a length of "
             << match.length(i) << endl;
    }
    return 0;
}
Output: 
match size is 3
match 0 has a length of 13
match 1 has a length of 5
match 2 has a length of 8

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :