Open In App

match_results prefix() and suffix() in C++

Last Updated : 09 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report
  • 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 parameters.
    Return Value: This function returns the sequence preceding the matched sequence in the target string.
    Note: First element always contains the whole regex match while the others contain the particular Capturing Group.
    Below program illustrate the above function: 
     

CPP




// 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 parameters.
    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 contain the particular Capturing Group.
    Below program illustrate the above function: 
     

CPP




// 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]

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads