Skip to content
Related Articles

Related Articles

match_results prefix() and suffix() in C++
  • Last Updated : 04 Sep, 2018
  • The match_results::prefix() is an inbuilt function in C++ which is used to get the string which is preceding the matched string in the input target string.

    Syntax:

    smatch_name.prefix()
    
    Note: smatch_name is an object of match_results class.
    

    Parameters: This function accept no paramters.

    Return Value: This function returns the sequence preceeding the matched sequence in the target string.

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

    Below program illustrate the above function:






    // CPP program to illustrate
    // match_results prefix() function
    #include <bits/stdc++.h>
    using namespace std;
      
    int main()
    {
        string s("Geeksforgeeks is a computer science portal");
        regex re("computer");
      
        smatch match;
      
        regex_search(s, match, re);
      
        cout << "Prefix is: [";
        if (!match.empty()) {
            cout << match.prefix() << "]" << endl;
        }
        return 0;
    }

    
    

    Output:

    Prefix is: [Geeksforgeeks is a ]
    
  • The match_results::suffix() is an inbuilt function in C++ which is used to get the string which is succeeding the matched string in the input target string.

    Syntax:

    smatch_name.suffix()
    
    Note: smatch_name is an object of match_results class.
    

    Parameters: This function accepts no paramters.

    Return Value:T his function returns the sequence succeeding the matched sequence in the target string.

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

    Below program illustrate the above function:




    // CPP program to illustrate
    // match_results suffix() function
    #include <bits/stdc++.h>
    using namespace std;
      
    int main()
    {
        string s("Geeksforgeeks is a computer science portal");
        regex re("computer");
      
        smatch match;
      
        regex_search(s, match, re);
      
        cout << "Suffix is: [";
        if (!match.empty()) {
            cout << match.suffix() << "]" << endl;
        }
        return 0;
    }

    
    

    Output:

    Suffix is: [ science portal]
    

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :