match_results begin() and end() function in C++ STL
- 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>usingnamespacestd;intmain(){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;return0;}chevron_rightfilter_noneOutput: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>usingnamespacestd;intmain(){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;return0;}chevron_rightfilter_noneOutput:Match size = 3 matches: matchresult match result
Recommended Posts:
- unordered_multiset begin() function in C++ STL
- valarray begin() function in C++
- unordered_multimap begin() and end() function in C++ STL
- multiset begin() and end() function in C++ STL
- unordered_set begin() function in C++ STL
- map::begin() and end() in C++ STL
- set::begin() and set::end() in C++ STL
- unordered_map begin() in C++
- list::begin() and list::end() in C++ STL
- deque::begin() and deque::end in C++ STL
- forward_list::begin() and forward_list::end() in C++ STL
- multimap::begin() and multimap::end() in C++ STL
- array::begin() and array::end() in C++ STL
- vector::begin() and vector::end() in C++ STL
- Function Overloading vs Function Overriding in C++
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.



