
- 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 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
- Related Articles
- match max_size() function in C++ STL
- Limit length of longtext field in MySQL SELECT results?
- MYSQL: Can you pull results that match like 3 out of 4 expressions?
- Determining the position and length of the match Java regex
- Counts of distinct consecutive sub-string of length two using C++ STL
- Analyzing Monitor Results in Postman
- How to concatenate results in MongoDB?
- How to PIVOT results in Oracle?
- How to UNPIVOT results in Oracle?
- How to match date with MongoDB $match?
- Match in Rust Programming
- Containers in C++ STL
- count_if() in C++ STL
- Library in C++ STL?
- stable_sort() in C++ STL
