Regular Expressions in C++ with Examples for Beginners

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: A Complete Guide for Beginners



Regular expressions (regex) are a powerful tool for searching, validating, and replacing text based on a pattern. In C++, working with regular expressions is implemented through the <regex> library, which was introduced in the C++11 standard. In this article, we will cover the basics of working with regex in C++ using practical examples that are understandable even for beginners.



What are regular expressions and why are they needed?



A regular expression is a sequence of characters that defines a pattern for searching in text. For example, the pattern \\d{3}-\\d{2}-\\d{2} describes the format of a social security number: three digits, a hyphen, two digits, a hyphen, two digits.



The main tasks solved by regular expressions in C++ include:



  • Input validation (email, phone number, date)
  • Extracting data from text
  • Searching and replacing substrings
  • Splitting strings by complex delimiters


The <regex> library provides three main algorithms:



  • std::regex_match — full string match against the pattern
  • std::regex_search — search for a match within a string
  • std::regex_replace — replace matches based on the pattern


Basic Syntax of Regular Expressions in C++



Before writing code, let's break down the basic elements of regex syntax that will be useful in the examples:



CharacterMeaningExample
.Any character (except newline)c.t — cat, cot, cut
\\dDigit (0-9)\\d\\d — 23, 45, 99
\\wLetter, digit, or underscore\\w+ — word
\\sWhitespace character\\s+ — one or more spaces
*0 or more repetitionsab*c — ac, abc, abbc
+1 or more repetitionsab+c — abc, abbc (not ac)
?0 or 1 repetitioncolou?r — color, colour
{n}Exactly n repetitions\\d{3} — 123, 456
[abc]Any character from the set[aeiou] — vowel
()Group for capturing(\\d{2})-(\\d{2})


Example 1: Email Address Validation Using regex_match



Let's start simple — check if a string matches an email format. We'll use std::regex_match, which requires a full match.



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

int main() { std::string email = "user@example.com"; // Simple pattern for email: // [\\w.]+ — one or more characters (letters, digits, dot, underscore) // @ — at symbol // [\\w.]+ — domain name // \\.[a-z]{2,} — dot and top-level domain (at least 2 letters) std::regex pattern(R"([\\w.]+@[\\w.]+\\.\\w{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 email = "user@com"; if (std::regex_match(email, pattern)) { std::cout << "Email is valid" << std::endl; } else { std::cout << "Email is invalid" << std::endl; } return 0;}


Output:

Email is validEmail is invalid


Note that

Blogs

Book Recommendations