
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
match_results prefix() and suffix() in C++
In this article we will be discussing the working, syntax and examples of match_results::prefix() and match_results::suffix() functions in C++ STL.
What is a match_results in C++ STL?
std::match_results is a specialized container-like class which is used to hold the collection of character sequences which are matched. In this container class a regex match operation finds the matches of the target sequence.
What is match_results:: prefix()?
match_results::prefix() function is an inbuilt function in C++ STL, which is defined in <regex> header file. prefix() is used to get the preceding match_results of the object associated with the function. This function returns the reference of the succeeding sequence of the match_results object.
Syntax
match_results.prefix();
Parameters
This function accepts no parameter.
Return value
This function returns constant reference of the string or sequence preceding the match sequence.
Example
Input: string str = "Tutorials Points"; regex R("Points"); smatch Mat; regex_search(str, Mat, R); Mat.prefix(); Output: Tutorials
prefix()
Example
#include <bits/stdc++.h> using namespace std; int main() { string str = "Tutorials Points"; regex R("Points"); smatch Mat; regex_search(str, Mat, R); cout<<"String prefix is : "; if (!Mat.empty()) { cout << Mat.prefix(); } return 0; }
Output
If we run the above code it will generate the following output −
String prefix is : Tutorials
What is match_results:: suffix()?
match_results::suffix() function is an inbuilt function in C++ STL, which is defined in <regex> header file. suffix() is used to get the succeeding match_results of the object associated with the function. This function returns the reference of the succeeding sequence of the match_results object.
Syntax
match_results.suffix();
Parameters
This function accepts no parameter.
Return value
This function returns constant reference of the string or sequence succeeding the match sequence.
Example
Input: std::string str("Tutorials Points is the best"); std::smatch Mat; std::regex re("Points"); std::regex_match ( str, Mat, re ); Mat.suffix(); Output: is the best
suffix()
Example
#include <bits/stdc++.h> using namespace std; int main() { string str = "Tutorials Points is the best"; regex R("Points"); smatch Mat; regex_search(str, Mat, R); cout<<"String prefix is : "; if (!Mat.empty()) { cout << Mat.suffix(); } return 0; }
Output
If we run the above code it will generate the following output −
String prefix is : is the best
- Related Articles
- Check if suffix and prefix of a string are palindromes in Python
- Program to find longest prefix that is also a suffix in C++
- How to Add Prefix or Suffix into Cell Values in Google Sheets?
- How to Add Prefix or Suffix to a Range of Cells in Excel
- Find the longest sub-string which is prefix, suffix and also present inside the string in Python
- How can I eradicate some specific suffix or prefix or both from a MySQL string?
- Find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated in Python
- Find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated in C++
- Program to find number of ways we can arrange letters such that each prefix and suffix have more Bs than As in Python
- Print the longest prefix of the given string which is also the suffix of the same string in C Program.
- MYSQL: Can you pull results that match like 3 out of 4 expressions?
- Suffix Array
- Prefix and Postfix Expressions in Data Structure
- Precedence of postfix ++ and prefix ++ in C/C++
- Difference between prefix and postfix operators in C#?
