Match Results Prefix And Suffix In C++ - C++ Programming Tutorial
C++ Course / Miscellaneous / Match Results Prefix And Suffix In C++

Match Results Prefix And Suffix In C++

BLUF: Mastering Match Results Prefix And Suffix In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Match Results Prefix And Suffix In C++

C++ is renowned for its efficiency. Learn how Match Results Prefix And Suffix In C++ enables low-level control and high-performance computing in the tutorial below.

In this tutorial, you will explore the prefix and suffix functions within the match_results class in C++. It is essential to understand Regular Expressions (Regex) in C++ before delving into the details of these functions. Let's start by examining the syntax and usage examples of prefix and suffix.

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's explore the std::prefix and suffix member functions further:

A matched substring or a section of the initial text that met the regular expression is denoted by the class template std::sub_match. It stores information about the part of the string that corresponds, including the length of the match and its location within the original string, along with other useful information.

You receive a std::submatch object when employing match.prefix or match.suffix, with match being an instance of std::matchresults. Subsequently, you can manipulate this std::sub_match object to access details regarding the prefix or suffix, such as the length of the text, its position, and additional data.

"prefix":

The std::submatch object is a method that provides access to the text preceding the matched substring. Information about the matched prefix is stored within the std::submatch object.

The match_results::prefix function in C++ is a predefined function designed to extract the substring preceding the matched substring within 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.

The function provides the value that comes before the matched sequence within the target string.

"suffix":

Similar to prefix, the suffix function returns an object that includes the text following the identified substring, referred to as std::sub match. This object provides specific information related to the respective suffix.

The match_results::suffix function is an intrinsic feature in C++ that is employed to extract the portion of the input string following the matched substring.

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.

The function returns the part of the target string that comes before the matched sequence.

Example:

Let's consider a scenario to demonstrate the functionality of the prefix and suffix methods in C++'s match_results.

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.

Utilize std::sregex_iterator to cycle through all occurrences of the regular expression pattern within the given text. Access Match.prefix to retrieve the text preceding the identified substring for each match, and utilize matching.suffix to obtain the text following the recognized substring.

The text consists of words that align with the standard pattern \\b\\w+\\b. The iteration displays the identified word, its preceding prefix, and its subsequent suffix for each word found.

It provides contextual details regarding the identified matches within the text, allowing you to extract the matched substrings as well as the preceding and subsequent text for 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:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience