Open In App

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

smatch_name.prefix()

Note: smatch_name is an object of match_results class.




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

 

smatch_name.suffix()

Note: smatch_name is an object of match_results class.




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

 


Article Tags :
C++