Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
smatch | Regex (Regular Expressions) in C++
Next article icon

smatch | Regex (Regular Expressions) in C++

Last Updated : 18 Jan, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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 ? Examples: 

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: 

CPP
#include <bits/stdc++.h> using namespace std; int main() {     string sp("geeksforgeeks");     regex re("(geeks)(.*)");      // flag type for determining the matching behavior     // && here it is for matches on strings.     smatch match;      // we can use member function on match     // to extract the matched pattern.     if (regex_search(sp, match, re) == true) {          // The size() member function indicates the         // number of capturing groups plus one for the overall match         // match size = Number of capturing group + 1         // (.*) which "forgeeks" ).         cout << "Match size = " << match.size() << endl;          // Capturing group is index from 0 to match_size -1          // .....here 0 to 2         // pattern at index 0 is the overall match "geeksforgeeks"         // pattern at index 1 is the first capturing group "geeks"         // pattern at index 2 is the 2nd capturing group "forgeeks"          cout << "Whole match : " << match.str(0) << endl;         cout << "First capturing group is '" << match.str(1)              << "' which is captured at index " << match.position(1)              << endl;         cout << "Second capturing group is '" << match.str(2)              << "' which is captured at index " << match.position(2)              << endl;     }     else {         cout << "No match is found" << endl;     }     return 0; } 
Output:
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

Time Complexity: O(n)
Auxiliary Space: O(1)


Next Article
smatch | Regex (Regular Expressions) in C++

V

vivek kumar 9
Improve
Article Tags :
  • C++
Practice Tags :
  • CPP

Similar Reads

    std::regex_match, std::regex_replace() | Regex (Regular Expression) In C++
    Regex is the short form for “Regular expression”, which is often used in this way in programming languages and many different libraries. It is supported in C++11 onward compilers.Function Templates used in regex regex_match() -This function return true if the regular expression is a match against th
    3 min read
    How to Validate MICR Code using Regular Expression?
    MICR stands for Magnetic Ink Character Recognition. This technology provides transaction security, ensuring the correctness of bank cheques. MICR code makes cheque processing faster and safer. MICR Technology reduces cheque-related fraudulent activities. Structure of a Magnetic Ink Character Recogni
    5 min read
    Validating Bank Account Number Using Regular Expressions
    A bank account number is a unique number that is assigned to the account holder after opening their account in any specific bank. In technical terms, we can consider the Bank account number as the Primary Key. A bank account number enables us to do debit, credit, and other transactions. As per RBI G
    6 min read
    regex_error in C++
    regex_error is present inside the Header "regex" and inside the Class regex_error;. It helps us to know about the errors which are thrown during the program execution, it defines the type of the object of exception in regular expressions library, Also describes an error in the construction or use of
    2 min read
    std::basic_istream::ignore in C++ with Examples
    The std::basic_istream::ignore is used to extracts characters from the input string and discards them including delimiting character, i.e., if the end of the file is reached this function stops extracting characters. The delimiting character is the new line character i.e '\n'. This function will als
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences