smatch | Regex (Regular Expressions) in C++

smatch is an instantiation of the match_results class template for matches on string objects.
Functions that can be called using smatch:
str(), position(), and length() member functions of the match_results object can be called to get the text that was matched, or the starting position and its length of the match relative to the subject string.

  • Call these member functions without a parameter or with 0 as the parameter to get the overall regex match.
  • Call them passing 1 or greater to get the match of a particular capturing group.
  • The size() member function indicates the number of capturing groups plus one for the overall match.
  • Thus you can pass a value up to size()-1 to the other three member functions(str(), position(), length() ).

What is capturing group ?

Suppose you create a regex object like : regex re("(geeks)(.*)") 
Here no of capturing group is = 2 
[ one is "geeks" and second is any character after "geeks" ].

regex re("a(b)c")
Here no of capturing group is = 1[ 'b' is the capturing group].
whatever within '(' and ')' braces is treated as capturing group.

Below is the program to show the working of smatch:





#include <bits/stdc++.h>
using namespace std;
int main()
    string sp("geeksforgeeks");
    regex re("(geeks)(.*)");
    // flag type for determining the matching behavior
    // && here it is for matches on strings.
    smatch match;
    // we can use member function on match
    // to extract the matched pattern.
    if (regex_search(sp, match, re) == true) {
        // 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;
        // Capturing group is index from 0 to match_size -1 
        // 0 to 2
        // pattern at index 0 is the overall match "geeksforgeeks"
        // pattern at index 1 is the first capturing group "geeks"
        // pattern at index 2 is the 2nd capturing group "forgeeks"
        cout << "Whole match : " << match.str(0) << endl;
        cout << "First capturing group is '" << match.str(1)
             << "' which is captured at index " << match.position(1)
             << endl;
        cout << "Second capturing group is '" << match.str(2)
             << "' which is captured at index " << match.position(2)
             << endl;
    else {
        cout << "No match is found" << endl;
    return 0;



Match size = 3
Whole match : geeksforgeeks
First capturing group is 'geeks' which is captured at index 0
Second capturing group is 'forgeeks' which is captured at index 5

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

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 or mail your article to 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 :


Please write to us at to report any issue with the above content.