smatch is an instantiation of the match_results class template for matches on string objects.
Functions that can be called using smatch:
str(), position(), and length() member functions of the match_results object can be called to get the text that was matched, or the starting position and its length of the match relative to the subject string.
- Call these member functions without a parameter or with 0 as the parameter to get the overall regex match.
- Call them passing 1 or greater to get the match of a particular capturing group.
- The size() member function indicates the number of capturing groups plus one for the overall match.
- Thus you can pass a value up to size()-1 to the other three member functions(str(), position(), length() ).
What is capturing group ?
Example-1: Suppose you create a regex object like : regex re("(geeks)(.*)") Here no of capturing group is = 2 [ one is "geeks" and second is any character after "geeks" ]. Example-2: regex re("a(b)c") Here no of capturing group is = 1[ 'b' is the capturing group]. whatever within '(' and ')' braces is treated as capturing group.
Below is the program to show the working of smatch:
Match size = 3 Whole match : geeksforgeeks First capturing group is 'geeks' which is captured at index 0 Second capturing group is 'forgeeks' which is captured at index 5
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- smatch max_size() function in C++ STL
- std::regex_match, std::regex_replace() | Regex (Regular Expression) In C++
- <regex> library in C++ STL
- Program to find all match of a regex in a string
- How to validate an IP address using ReGex
- A Puzzle on C/C++ R-Value Expressions
- Generalized Lambda Expressions in C++14
- How to validate MAC address using Regular Expression
- How to validate Indian driving license number using Regular Expression
- How to validate CVV number using Regular Expression
- Remove duplicate words from Sentence using Regular Expression
- How to validate PAN Card number using Regular Expression
- How to validate time in 24-hour format using Regular Expression
- How to validate pin code of India using Regular Expression
- How to validate Hexadecimal Color Code using Regular Expression
- How to check string is alphanumeric or not using Regular Expression
- How to check Aadhar number is valid or not using Regular Expression
- How to validate image file extension using Regular Expression
- How to validate a domain name using Regular Expression
- How to validate GST (Goods and Services Tax) number using Regular Expression
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.