How to Match a Pattern in a String in C++? Last Updated : 04 Mar, 2024 Comments Improve Suggest changes Like Article Like Report In C++, strings are sequences of characters stored in a char array. Matching a pattern in a string involves searching for a specific sequence of characters (the pattern) within a given string. In this article, we will learn how to match a pattern in a string in C++. Example: Input:Text: "GeeksForGeeks"Pattern: "(Geek)(.*)" // Geek followed by any characterOutput:Pattern matched!Pattern Matching in C++To match a pattern in a string we can use regular expressions, which provide a powerful and flexible way to describe and match patterns within strings. C++ provides the <regex> library, which offers support for regular expressions, allowing for more complex pattern matching. Approach:Use the std::regex class to create a regular expression object, passing the pattern as a parameter to the constructor.Use std::regex_search or std::regex_match to search for or match the pattern within the string.If a match is found print pattern matched.C++ Program to Match a Pattern in a String The below program demonstrates how we can match a pattern in a string in C++. C++ // C++ Program to show how to match a pattern in a string #include <iostream> #include <regex> #include <string> using namespace std; int main() { string text = "GeeksForGeeks"; // Define the regular expression pattern to match regex pattern( "(Geek)(.*)"); // Geek followed by any character // Check if the entire string matches the pattern if (regex_match(text, pattern)) { // If matched, print message cout << "Pattern found!" << endl; } else { // If not matched, print message cout << "Pattern not found!" << endl; } return 0; } OutputPattern found! Time Complexity: O(N), here N is the size of the input string.Auxiliary Space: O(1) Note: For larger texts or more complex patterns, consider using more efficient string matching algorithms like the Knuth-Morris-Pratt (KMP) algorithm or the Boyer-Moore algorithm. These algorithms can handle larger inputs more effectively. Comment More infoAdvertise with us Next Article How to Match a Pattern in a String in C++? susobhanakhuli19 Follow Improve Article Tags : C++ Programs C++ cpp-string CPP Examples Practice Tags : CPP Similar Reads How to Split a String into an Array in C++? In C++, splitting a string into an array of substrings means we have to parse the given string based on a delimiter and store each substring in an array. In this article, we will learn how to split a string into an array of substrings in C++. Example: Input: str= âHello, I am Geek from geeksforgeeks 2 min read How to Read File into String in C++? In C++, file handling allows us to read and write data to an external file from our program. In this article, we will learn how to read a file into a string in C++. Reading Whole File into a C++ StringTo read an entire file line by line and save it into std::string, we can use std::ifstream class to 2 min read How to Replace Text in a String Using Regex in C++? Regular expressions or what we can call regex for short are sequences of symbols and characters that create a search pattern and help us to find specific patterns within a given text. In this article, we will learn how to replace text in a string using regex in C++. Example Input: str = "Hello, Worl 2 min read How to Split a C++ String into a Vector of Substrings? In C++, splitting a string into a vector of substrings means we have to split the given string into a substring based on a given delimiter and store each substring in a vector. In this article, we will learn how to split a string into a vector of substrings in C++. Example: Input: str= "Hello, I am 2 min read How to Parse Mathematical Expressions in a C++ String? In C++, strings are sequences of characters stored in a char array. Strings are used to store words and text. We can also store mathematical expressions in this string. In this article, we will explore how to parse mathematical expressions in a C++ String. Example: Input:expression = (3 + 4) * 2 / ( 5 min read How to Take Multiple Line String Input in C++? In C++, taking string input is a common practice but the cin is only able to read the input text till whitespace. In this article, we will discuss how to read the multiple line of text input in C++. For Example, Input:Enter Your Text: This is amultiline text.Output:You Have Entered:This is amultilin 2 min read How to Split a String by a Delimiter in C++? Splitting a string is the process of dividing the given string into multiple substrings on the basis of a character (or substring) as the separator. This separator is called delimiter and the whole process is also called tokenization.ExamplesInput: str = "geeks,for,geeks", delimiter = (,)Output: gee 4 min read How Do I Iterate Over the Words of a String? In C++, strings are character sequences stored in a char array. It may contain text in the form of multiple words representing a sentence. In this article, we will learn how we can iterate over the words of a string in C++. Example: Input: myString ="Geek for Geeks" Output: Geek for GeeksIterate Ove 2 min read How to Convert a std::string to char* in C++? In C++, strings are the textual data that is represented in two ways: std::string which is an object, and char* is a pointer to a character. In this article, we will learn how to convert a std::string to char* in C++. Example Input:string myString = "GeeksforGeeks";Output:char * myString = "Geeksfor 1 min read How to Take Multiple String Inputs in a Single Line in C++? In C++, strings are used to store textual data. While working with user input, we often may need to take multiple string inputs in a single line only. In this article, we will learn how to take multiple string inputs in a single line. Example Input: Hi,Geek,Welcome,to,GfG delimiter = ',' Output: Str 2 min read Like