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++
  • Standard Template Library
  • STL Vector
  • STL List
  • STL Set
  • STL Map
  • STL Stack
  • STL Queue
  • STL Priority Queue
  • STL Interview Questions
  • STL Cheatsheet
  • C++ Templates
  • C++ Functors
  • C++ Iterators
Open In App
Next Article:
std::regex_match, std::regex_replace() | Regex (Regular Expression) In C++
Next article icon

std::regex_match, std::regex_replace() | Regex (Regular Expression) In C++

Last Updated : 04 Jul, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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 the given string otherwise it returns false.

CPP
// C++ program to demonstrate working of regex_match() #include <iostream> #include <regex>  using namespace std; int main() {     string a = "GeeksForGeeks";      // Here b is an object of regex (regular expression)     regex b("(Geek)(.*)"); // Geek followed by any character      // regex_match function matches string a against regex b     if ( regex_match(a, b) )         cout << "String 'a' matches regular expression 'b' \n";      // regex_match function for matching a range in string      // against regex b     if ( regex_match(a.begin(), a.end(), b) )         cout << "String 'a' matches with regular expression "                 "'b' in the range from 0 to string end\n";      return 0; } 

Output
String 'a' matches regular expression 'b'  String 'a' matches with regular expression 'b' in the range from 0 to string end 

regex_search() - This function is used to search for a pattern matching the regular expression 

CPP
// C++ program to demonstrate working of regex_search() #include <iostream> #include <regex> #include<string.h> using namespace std;  int main() {     // Target sequence     string s = "I am looking for GeeksForGeeks "                "articles";      // An object of regex for pattern to be searched     regex r("Geek[a-zA-Z]+");      // flag type for determining the matching behavior     // here it is for matches on 'string' objects     smatch m;      // regex_search() for searching the regex pattern     // 'r' in the string 's'. 'm' is flag for determining     // matching behavior.     regex_search(s, m, r);      // for each loop     for (auto x : m)         cout << x << " ";      return 0; } 

Output
GeeksForGeeks 

regex_replace() This function is used to replace the pattern matching to the regular expression with a string.

CPP
// C++ program to demonstrate working of regex_replace() #include <iostream> #include <string> #include <regex> #include <iterator> using namespace std;  int main() {      string s = "I am looking for GeeksForGeek \n";          // matches words beginning by "Geek"     regex r("Geek[a-zA-z]+");          // regex_replace() for replacing the match with 'geek'      cout << std::regex_replace(s, r, "geek");          string result;          // regex_replace( ) for replacing the match with 'geek'     regex_replace(back_inserter(result), s.begin(), s.end(),                   r,  "geek");      cout << result;      return 0; } 

Output
I am looking for geek  I am looking for geek  

So Regex operations make use of following parameters :

  • Target sequence (subject) – The string to be matched.
  • Regular Expression (Pattern) – The regular expression for the target sequence.
  • Matched Array – The information about matches is stored in a special match_result array.
  • Replacement String – These string are used for allowing replacement of the matches.


Next Article
std::regex_match, std::regex_replace() | Regex (Regular Expression) In C++

K

kartik
Improve
Article Tags :
  • Strings
  • C Language
  • C++
  • DSA
  • Microsoft
  • STL
  • CPP-Library
  • CPP-regex
Practice Tags :
  • Microsoft
  • CPP
  • STL
  • Strings

Similar Reads

    smatch | Regex (Regular Expressions) in C++
    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 lengt
    3 min read
    Match flight number using Regular Expression
    Given some Flight Numbers, the task is to check if they are valid or not using regular expressions. Rules for the valid flight numbers are: It is an alphanumeric string containing uppercase Alphabet letters(A-Z) and digits(0-9).It should always start with one of the alphabet letters and should end w
    6 min read
    Count occurrences of a word in string | Set 2 (Using Regular Expressions)
    Given a string str and a word w, the task is to print the number of the occurrence of the given word in the string str using Regular Expression. Examples: Input: str = "peter parker picked a peck of pickled peppers”, w = "peck"Output: 1Explanation: There is only one occurrence of the word "peck" in
    4 min read
    Regular Expression Matching
    Given a text t and a pattern p where t consists of only lowercase English alphabets while p consists of lowercase English alphabets as well as special characters '.' and '*', the task is to implement a function to test regular expression such that:'.' Matches any single character.​​​​'*' Matches zer
    14 min read
    Regex Tutorial - How to write Regular Expressions?
    A regular expression (regex) is a sequence of characters that define a search pattern. Here's how to write regular expressions: Start by understanding the special characters used in regex, such as ".", "*", "+", "?", and more.Choose a programming language or tool that supports regex, such as Python,
    6 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