Match Results Prefix And Suffix In C++

In this article, you will learn about the match_results prefix and suffix in C++ with their syntaxes and examples. But before going to the prefix and suffix function, you must know about the Regex expression in C++.

Regular expressions supplied by the <regex> header are used in conjunction with the std::matchresults class template in C++. The results of comparing a regular expression with a string are represented by the variable matchresults . The <regex> header in C++ offers functions for working with regular expressions. The results of a match between a regular expression & a string are represented by the class template std::match_results.

  • The std::match_results class has member procedures called prefix and suffix that give gain access to the prefix and suffix of the matched text, accordingly

Let us examine the std::prefix and suffix member functions in more detail:

A matched substring or a segment of the original text that matched the regular expression is represented by the class template std::sub_match . It saves details about the portion of the string that matches, such as the match's length and position inside the original string, among other helpful data.

You get an object called std::submatch when you use match. prefix or match.suffix , where match is an object of std::matchresults. After that, you can deal with this std::sub_match object to obtain information about the prefix or suffix, including the text's length, location, and other data.

"prefix":

The std::submatch object is a member function that returns the text that comes before the matched substring. The matched prefix's details are contained in the std::submatch.

The match_results::prefix is a built-in C++ function that is used to retrieve the string in the input target string that comes before the matched string.

Syntax:

It has the following syntax:

Example

smatch_name.prefix()

Note: smatch_name is an object of match_results class.

Parameters: This function accepts no parameters.

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

"suffix":

Like prefix, suffix yields an object containing the text that comes after the matched substring, std::sub match. It has details regarding the corresponding suffix.

The match_results::suffix is a built-in C++ function that is used to retrieve the string that appears after the matched string in the input target string.

Syntax:

It has the following syntax:

Example

smatch_name.prefix()

Note: smatch_name is an object of match_results class.

Parameters: This function accepts no parameters.

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

Example:

Let us take an example to illustrate the use of match_results prefix and suffix function in C++.

Example

#include <iostream>
#include <regex>
int main() 
{
    std::string text = "Hello, World! This is a sample text.";
    std::regex pattern("\\b\\w+\\b"); // Regular expression to match words
    std::sregex_iterator iter(text.begin(), text.end(), pattern);
    std::sregex_iterator end;
    for (; iter != end; ++iter) 
   {
        std::smatch match = *iter;
        std::cout << "Matched: " << match.str() << std::endl;
        std::cout << "Prefix: " << match.prefix() << std::endl;
        std::cout << "Suffix: " << match.suffix() << std::endl;
        std::cout << "-----------------" << std::endl;
    }
    return 0;
}

Output:

Output

Matched: Hello
Prefix: 
Suffix: World! This is a sample text.
-----------------
Matched: World
Prefix: , 
Suffix:! This is a sample text.
-----------------
Matched: This
Prefix:! 
Suffix:  is a sample text.
-----------------
Matched: is
Prefix:  
Suffix:  a sample text.
-----------------
Matched: a
Prefix:  
Suffix:  sample text.
-----------------
Matched: sample
Prefix:  
Suffix:  text.
-----------------
Matched: text
Prefix:  
Suffix:
-----------------

Explanation:

  • A std::smatch object reflecting the current match is returned by *iter .
  • The word, or actual matched substring, is retrieved using match. str.
  • The matched word's prefix and suffix can be accessed using prefix and match.suffix.

Use std::sregex_iterator to iterate over all matches of the regular expression pattern in the provided text. Match.prefix returns the text that comes before the matched substring for each match while matching.suffix gets the text that comes after the matched substring.

The content contains words that the regular phrase \\b\\w+\\b matches. The loop prints the matching word, its prefix (text that comes before the word), and its suffix (text that comes after the word) for each word that is discovered.

It gives you contextual information about the matches detected in the text in addition to enabling you to extract the matched substrings and the text that appears before and after each match.

Conclusion:

  • Use the prefix function to obtain the text that comes before the matched substring.
  • Use the suffix function to get the content that follows the matched substring.
  • These functions give you the ability to examine or work with the surrounding text in addition to the matches, which is especially helpful when you need to extract more context or information about the matched substrings.
  • You can extract specific information from a given string in C++ together with its context by using these functions in conjunction with regular expressions to handle more sophisticated text-processing tasks with efficiency.

Input Required

This code uses input(). Please provide values below: