
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Match Results Length in C++ STL
In this article we will be discussing the working, syntax and examples of match_results::length() function 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::length()?
match_results::length() function is an inbuilt function in C++ STL, which is defined in <regex> header file. length() is used to check the length of the n-th match in the match_results object associated with it. length() accepts a parameter which is the match number which should be less than match_results::size(), for checking the length of the nth match.
Syntax
smatch_name.length(unsigned int num);
Parameters
This function accepts one parameter which is the match number which should be lower than the size of the container. Match number 0 represents the entire match expression.
Return value
This function returns unsigned integer value of the number of matches in the object
Example
Input: std::smatch; smatch.length(0); Output: 0
Example
#include <bits/stdc++.h> using namespace std; int main() { string str = "TutorialsPoint"; regex R("(Tutorials)(.*)"); smatch Mat; regex_match(str, Mat, R); for (int i = 0; i < Mat.size(); i++) { cout<<"Match is : " << Mat[i]<< endl; } return 0; }
Output
If we run the above code it will generate the following output −
Match is : TutorialsPoint Match is : Tutorials Match is : Point
Example
#include <bits/stdc++.h> using namespace std; int main() { string sr = "Tutorials Point"; regex Re("(Tutorials)(.*)"); smatch Mat; regex_match(sr, Mat, Re); int len = 0; string str; for (int i = 1; i < Mat.size(); i++) { if (Mat.length(i) > len) { str = Mat[i]; len = Mat.length(i); } } cout<<"Match length is of: " << len; return 0; }
Output
If we run the above code it will generate the following output −
Match length is of: 9