Skip to content
Related Articles

Related Articles

match_results begin() and end() function in C++ STL
  • Last Updated : 04 Sep, 2018
  • match_results::cbegin() is an inbulit function in C++ STL which returns a iterator that points to the first match in the match_results object.

    Syntax:

    smatch_name.begin()

    Parameters: This function does not accept any parameters.

    Return value: This function returns an iterator pointing to the first match in the match_results object. The matches contained in the match_results object are always constant.

    Note: First element always contains the whole regex match while the others contain the particular Capturing Group.

    Below program illustrate the above function:






    // C++ program to illustrate the 
    // match_results begin() function 
    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
        string sp("geeksforgeeks");
        regex re("(geeks)(.*)");
      
        smatch match;
      
        // we can use member function on match
        // to extract the matched pattern.
        std::regex_match(sp, match, re);
      
        // The size() member function indicates the
        // number of capturing groups plus one for the overall match
        // match size = Number of capturing group + 1
        // (.*) which "forgeeks" ).
        cout << "Match size = " << match.size() << endl;
      
        cout << "matches:" << endl;
        for (smatch::iterator it = match.begin(); it != match.end(); ++it)
            cout << *it << endl;
        return 0;
    }
    Output:
    Match size = 3
    matches:
    geeksforgeeks
    geeks
    forgeeks
    
  • match_results::end() is an inbulit function in C++ STL which returns an iterator that points to the past-the-end match in the match_results object.

    Syntax:

    smatch_name.end()

    Parameters: This function does not accept any parameters.

    Return value: This function returns an iterator pointing to the past-the-end match in the match_results object. The matches contained in the match_results object are always constant.

    Note: First element always contains the whole regex match while the others contain the particular Capturing Group.

    Below program illustrate the above function




    // C++ program to illustrate the 
    // match_results end() function 
    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
        string sp("matchresult");
        regex re("(match)(.*)");
      
        smatch match;
      
        // we can use member function on match
        // to extract the matched pattern.
        std::regex_match(sp, match, re);
      
        // The size() member function indicates the
        // number of capturing groups plus one for the overall match
        // match size = Number of capturing group + 1
        // (.*) which "results" ).
        cout << "Match size = " << match.size() << endl;
      
        cout << "matches:" << endl;
        for (smatch::iterator it = match.begin(); it != match.end(); ++it)
            cout << *it << endl;
        return 0;
    }
    Output:
    Match size = 3
    matches:
    matchresult
    match
    result
    
    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.
    My Personal Notes arrow_drop_up
Recommended Articles
Page :