Regular Expressions in C++: Complete Guide with Examples

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++: A Powerful Tool for String Manipulation

Regular expressions (regex) are a formal language for searching and manipulating substrings in text based on patterns. In C++, support for regular expressions appeared in the C++11 standard and is implemented in the <regex> library. This is an indispensable tool for data validation, log parsing, pattern searching, and text replacement.

In this article, we will break down the main functions of the regex library, learn how to compose patterns, and look at practical code examples.



1. Main Types and Functions of the <regex> Library

Three main classes are used for working with regular expressions in C++:

  • std::regex — represents the regular expression itself (the pattern).
  • std::smatch — a container for storing search results (for strings of type std::string).
  • std::cmatch — an analogue for C-strings (const char*).

Key functions for applying regex:

  • std::regex_match() — checks if the entire string matches the pattern.
  • std::regex_search() — searches for the first match of the pattern in the string.
  • std::regex_replace() — replaces all matches with the specified text.


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

int main() { std::string text = "My number: +7-123-456-78-90"; std::regex pattern(R"(\\+7-\\d{3}-\\d{3}-\\d{2}-\\d{2})");

if (std::regex_search(text, pattern)) { std::cout << "Number found!" << std::endl; }

// Extract the match std::smatch match; if (std::regex_search(text, match, pattern)) { std::cout << "Found number: " << match[0] << std::endl; }

return 0;}


2. Regular Expression Syntax: Metacharacters and Quantifiers

Regex patterns in C++ use a syntax similar to ECMAScript (by default). Here are the basic elements:

  • Dot (.) — any single character except a newline.
  • Asterisk (*) — 0 or more repetitions of the previous character.
  • Plus (+) — 1 or more repetitions.
  • Question mark (?) — 0 or 1 repetition (makes the character optional).
  • Square brackets ([]) — character class (e.g., [0-9] — any digit).
  • Parentheses (()) — grouping and capturing a substring.
  • Vertical bar (|) — logical OR.
  • Caret symbol ^ — start of the string.
  • Dollar symbol $ — end of the string.

Examples of quantifiers:

  • \\d{3} — exactly 3 digits.
  • \\w{2,4} — from 2 to 4 alphanumeric characters.
  • [A-Za-z]+ — one or more Latin letters.


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

int main() { std::string email = "user@example.com"; // Simple pattern for email std::regex email_pattern(R"([\\w.+-]+@[\\w-]+\\.[\\w.]+)");

if (std::regex_match(email, email_pattern)) { std::cout << "Email is valid." << std::endl; } else { std::cout << "Email is invalid." << std::endl; }

// Phone number validation std::string phone = "+7 (495) 123-45-67"; std::regex phone_pattern(R"(\\+7\\s?\\(?\\d{3}\\)?\\s?\\d{3}-?\\d{2}-?\\d{2})");

if (std::regex_match(phone, phone_pattern)) { std::cout << "Phone number is valid." << std::endl; }

return 0;}


3. Extracting Data Using Capture Groups

Capture gr

Blogs

Book Recommendations