Regular Expressions in C++ with Examples: A Practical Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Regular Expressions in C++ with Examples: From Basics to Practice



Regular expressions (regex) are a powerful tool for searching, extracting, and replacing text fragments by pattern. In C++, support for regular expressions appeared in the C++11 standard and is implemented in the <regex> library. In this article, we will analyze practical examples of using std::regex that will help you efficiently process strings in your projects.



1. Basics of std::regex: Creation and Simple Search



Three main classes are used to work with regular expressions in C++: std::regex (pattern), std::smatch (result for a string), and std::sregex_iterator (iterator for multiple matches).



Example 1: Checking if a string matches a pattern

#include <iostream>#include <regex>#include <string>

int main() { std::string email = "user@example.com"; // Pattern for checking email (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 does not match the format." << std::endl; } return 0;}


The std::regex_match function checks whether the entire string completely matches the pattern. Note the use of R"(...)" — this is a raw string literal, which allows you to avoid escaping backslashes.



Example 2: Searching for a substring

#include <iostream>#include <regex>#include <string>

int main() { std::string text = "My number: +7 (123) 456-78-90. Call me!"; 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;}


std::regex_search searches for the first match of the pattern in the string. The result is stored in a std::smatch object, from which you can extract the found substring and its position.



2. Extracting Data Using Capturing Groups



Groups allow you to extract individual parts of a match. They are denoted by parentheses ().



Example 3: Parsing a date

#include <iostream>#include <regex>#include <string>

int main() { std::string date = "Today is 15.04.2025"; std::regex date_pattern(R"((\\d{2})\\.(\\d{2})\\.(\\d{4}))"); std::smatch match; if (std::regex_search(date, match, date_pattern)) { std::cout << "Full date: " << match[0] << std::endl; std::cout << "Day: " << match[1] << std::endl; std::cout << "Month: " << match[2] << std::endl; std::cout << "Year: " << match[3] << std::endl; } return 0;}


Example 4: Extracting all matches using an iterator

#include <iostream>#include <regex>#include <string>

int main() { std::string log = "ERROR: file not foundWARNING: low batteryINFO: operation completedERROR: connection timeout"; std::regex error_pattern(R"(^ERROR:\\s*(.+)$)"); // Use std::sregex_iterator to iterate over all lines auto words_begin = std::sregex_iterator(log.begin(), log.end(), error_pattern); auto words_end = std::sregex_iterator(); std::cout << "Found

Blogs

Book Recommendations