Regular Expressions in C++: A Powerful Tool for Text Processing
Regular expressions (regex) are a powerful tool for searching, extracting, and replacing text fragments based on a given pattern. In C++, support for regular expressions was introduced in the C++11 standard and is implemented in the <regex> header file. In this article, we will thoroughly examine how to use regular expressions in C++ with practical examples.
Main Components of the <regex> Library
The <regex> library includes several key classes and functions:
std::regex— a class representing a compiled regular expression.std::smatch— a container for storing search results (for strings of typestd::string).std::regex_match— checks whether the entire string matches the pattern.std::regex_search— searches for the first occurrence of the pattern in the string.std::regex_replace— replaces matches with the specified text.
Let's examine each of these components with examples.
Example 1: Checking if a String Matches a Pattern (regex_match)
The std::regex_match function returns true if the entire string completely matches the regular expression. This is ideal for data validation, such as checking an email or phone number.
#include <iostream>#include <regex>#include <string>
int main() { std::string email = "user@example.com"; // Pattern for email validation (simplified) std::regex pattern(R"([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,})"); if (std::regex_match(email, pattern)) { std::cout << "Email is valid." << std::endl; } else { std::cout << "Email is invalid." << std::endl; } // Example with an invalid email std::string bad_email = "user@.com"; if (std::regex_match(bad_email, pattern)) { std::cout << "Email is valid." << std::endl; } else { std::cout << "Email is invalid." << std::endl; } return 0;}In this example, we used a raw string R"(...)" to avoid escaping backslashes. The pattern checks that the string contains a local part, the @ symbol, a domain name, and a top-level domain.
Example 2: Searching for a Substring in Text (regex_search)
If you need to find the first occurrence of a pattern inside a string, std::regex_search is used. The results are stored in a std::smatch object.
#include <iostream>#include <regex>#include <string>
int main() { std::string text = "My phone number: +7 (123) 456-78-90. Call me!"; // Pattern for searching a Russian phone number std::regex phone_pattern(R"(\\+7\\s?\\(?\\d{3}\\)?\\s?\\d{3}[- ]?\\d{2}[- ]?\\d{2})"); std::smatch match; if (std::regex_search(text, match, phone_pattern)) { std::cout << "Found number: " << match.str() << std::endl; std::cout << "Position: " << match.position() << std::endl; } else { std::cout << "Number not found." << std::endl; } return 0;}The match object contains not only the found string but also information about the position of the occurrence. The match.str() method returns the found fragment, and match.position() returns the index of the start of the match in the original string.
Example 3: Replacing Text with regex_replace
The std::regex_replace function replaces all occurrences of the pattern with the specified string. Backreferences can be used (backrefer