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::match_results class template in C++. The results of comparing a regular expression with a string are represented by the variable match_results. 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.
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::sub_match when you use match. prefix() or match.suffix(), where match is an object of std::match_results. 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::sub_match 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::sub_match. 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: 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: 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++. 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:
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:
Next Topicmultimap::count() in C++
|