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

  • 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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;
    }

    chevron_right

    
    

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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;
    }

    chevron_right

    
    

    Output:

    Suffix is: [ science portal]
    


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 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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.