Updated April 5, 2023
Introduction to Regular Expressions in C++
Regular Expressions in C++ are an important part of programming which helps is finding a pattern that consists of a sequence of characters. It is also known as “ Regex “ and it is used to define a pattern that can be used in string searching algorithms. They can also be used to denote a string syntax. Each and every regular expression character will have a character or metacharacter with a meaning. Regex is a standardised method for a matching sequence of characters with patterns. Different types of Regular Expression can be performed using functions as well as iterators.
Functions of Regular Expressions in C++
Let’s have a look at the syntax for Regex in programming through function templates :
1. regex_search()
With the help of this function we can search for a pattern in a string that matches the given regular expression in the code.
Here is the C++ code to demonstrate the working of regex_search() function :
#include <iostream>
#include <regex>
#include<string.h>
using namespace std ;
int main()
{
string search_string = " Hello! Welcome to EDUCBA, Learn with the best " ;
regex regular_exp( "l[a-z_]+" ) ;
smatch sm ;
regex_search( search_string , sm , regular_exp ) ;
cout << " Given below string matches with the given pattern : " << endl ;
for ( auto i : sm )
cout << i << " " ;
return 0 ;
}
Output:
As you can see in the above code, we are including an additional header file with name <regex> so that we can use regular expression functionalities in our code. After that, in the main class, we are declaring a string with name “ search_string ” in that we are passing a text statement that can be searched later. Now we declared a regular expression that needs to be searched and the name of the regex is “ regular_exp ” in which we are adding “ l [a-z] ” which means this regular expression will search character “ l ” from the declared string. For matching behavior, we are also declaring a flag with the name “ sm ”. Finally, the regular expression search that will help in finding the search pattern In regular string. Similarly, through out we are displaying the matching pattern and for loop is used to traverse the string character by character.
2. regex_match()
With the help of this function we can match a pattern in a string that matches the given regular expression in the code.
Here is the C++ code to demonstrate the working of regex_match() function:
#include <iostream>
#include <string>
#include <regex>
using namespace std ;
int main () {
if ( regex_match ( "softwareDevelopment" , regex( "(Soft)(.*)" ) ) )
cout << "String:literal => matched\n" ;
const char search_string[] = "SoftwareDevelopmentHelp" ;
string text ( "software" ) ;
regex str_expr ( "(soft)(.*)" ) ;
if ( regex_match (text,str_expr) )
cout << "String:object => matched\n" ;
if ( regex_match ( text.begin() , text.end() , str_expr ) )
cout << "String:range(begin-end)=> matched\n" ;
cmatch cm ;
regex_match ( search_string , cm , str_expr ) ;
smatch sm ;
regex_match ( text ,sm , str_expr ) ;
regex_match ( text.cbegin(), text.cend(), sm, str_expr ) ;
cout << "String:range, size:" << sm.size() << " are the matches\n" ;
regex_match ( search_string, cm, str_expr, regex_constants::match_default ) ;
cout << "The total matches are : " ;
for ( int i = 0 ; i < sm.size() ; ++i ) {
cout << "[" << sm[i] << "] " ;
}
cout << endl ;
return 0 ;
}
Output:
As you can see in the above code, we are including an additional header file with name <regex> so that we can use regular expression functionalities in our code. Therefore, first we are matching the given string “softwareDevelopment” with the regular expression “(“(soft)(.*)” using the regex_match function. After that, we are also demonstrating different variations of regex_match by passing it a string object, range, etc.
3. regex_replace()
With the help of this function we can replace a pattern in a string that matches the given regular expression in the code.
Here is the C++ code to demonstrate the working of regex_replace() function:
#include <iostream>
#include <string>
#include <regex>
#include <iterator>
using namespace std;
int main()
{
string search_string = " Hello! Welcome to EDUCBA, Learn with the best \n " ;
cout << " Here is the input string: " << search_string << endl ;
regex regexp( "H[a-zA-z]+" ) ;
cout << " Replace the word 'Hello' with word 'Hey' : " ;
cout << regex_replace( search_string , regexp , "Hey" ) ;
string output ;
cout << " Replace the word 'Hey' back to 'Hello' : " ;
regex_replace( back_inserter(output) , search_string.begin() , search_string.end() ,
regexp , "Hello" ) ;
cout << output ;
return 0 ;
}
Output:
As you can see in the above code, we are including an additional header file with name <regex> so that we can use regular expression functionalities in our code. After that, in the main class, we are declaring a string with name “ search_string ” in that we are passing a text statement that can be searched later. Then we are using regex to match the string beginning with ‘H’. After that, we are using regex_replace() for replacing the match string with the word ‘Hey’. Then again, we are using regex_replace( ) for replacing the match string back with the word ‘Hello’. In the end, you can see on the output scree that how we have replaced the Hello word with Hey then again we replaced Hey string again with Hello string in the code.
Conclusion
Regular expressions play an important role in programming that helps in saving searching strings texts and patter in large and complex code in a small amount of time with ease. In addition, we can match, replace and search a given string by using regular expression in our code.
Recommended Articles
This is a guide to Regular Expressions in C++. Here we also discuss the Introduction and functions of regular expressions in c++ which includes,regex_search(),regex_match(),etc . You may also have a look at the following articles to learn more –