match_results cbegin() add cend() in C++ STL

  • match_results::cbegin() is an inbulit function in C++ STL which returns a constant iterator that points to the first match in the match_results object. This iterator cannot modify the contents of the vector.
    Syntax:

    smatch_name.cbegin()

    Parameters: This function does not accept any parameters.

    Return value: This function returns a constant 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.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to illustrate the 
    // match_result 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.cbegin(); it != match.cend(); ++it)
            cout << *it << endl;
        return 0;
    }

    chevron_right

    
    

    Output:

    Match size=3
    matches:
    geeksforgeeks
    geeks
    forgeeks
    
  • smatch::cend() is an inbulit function in C++ STL which returns a constant iterator that points to the past-the-end match in the match_results object. This iterator cannot modify the contents of the vector.

    Syntax:

    smatch_name.cend()

    Parameters: This function does not accept any parameters.

    Return value: This function returns a constant 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

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to illustrate the 
    // match_results cend() 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.cbegin(); it != match.cend(); ++it)
            cout << *it << endl;
        return 0;
    }

    chevron_right

    
    

    Output:

    Match size=3
    matches:
    matchresults
    match
    results
    


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.