⚙️ C++

Expressions régulières en C++ : guide détaillé

What is it and why? (a real-life analogy)

Imagine you are a librarian in a huge library containing millions of books. A reader comes to you and says: "I need all the books with the word 'adventures' in the title, but only those written after 2000, and with a red cover." If you had to look through every book manually, it would take weeks. But if you had a magic magnifying glass that instantly finds books based on such complex descriptions — that is regular expressions.

In the programming world, regular expressions (or regex, short for regular expressions) are a powerful tool for searching and processing text. They allow you to specify not just a specific word, but a pattern that can match many different strings.

Simple analogy: If a regular search is like saying "find me the word 'cat'", then a regular expression is like "find me any three-letter word that starts with 'c' and ends with 't'". The second option will find not only "cat", but also "cot", "cut" (if such a word exists) and any other combinations.

In C++, regular expressions appeared in the C++11 standard and have since become an indispensable tool for:

  • Data validation — checking if an email, phone number, or zip code is entered correctly
  • Search and replace — finding all dates in a text and changing their format
  • Log parsing — extracting needed information from thousands of lines of system records
  • Lexical analysis — breaking down program code into its constituent parts

Theory: how it works internally

To understand how regular expressions work, you need to imagine a finite automaton. Don't be scared by this term — it's actually quite simple.

Imagine you have a map with cities and roads between them. You start your journey in the city "Start" and move along the roads, reading the letters of the text. Each letter is a signpost indicating which road to take next. If, after reading the entire string, you end up in the city "Finish" — then the string matches the pattern. If you get lost or don't reach the finish — the string does not match.

A regular expression is compiled into such an automaton inside the C++ library. When you pass a string for checking, the regex engine goes through it character by character, trying to find a path from start to finish according to your pattern.

Basic building blocks of regular expressions:

Symbol Meaning Example Explanation
. Any single character c.t Finds "cat", "cot", "cut"
* 0 or more repetitions ca*t Finds "ct", "cat", "caat", "caaat"
+ 1 or more repetitions ca+t Finds "cat", "caat", but not "ct"
? 0 or 1 repetition ca?t Finds "ct" and "cat"
[] Set of characters [cb]at Finds "cat" and "bat"
[^] Negation of the set [^c]at Finds "bat", "rat", but not "cat"
^ Start of string ^cat Finds "cat" only at the beginning of a string
$ End of string cat$ Finds "cat" only at the end of a string
| Or (alternation) cat|dog Finds either "cat" or "dog"
() Grouping (ca)+t Finds "cat", "cacat", "cacacat"
d Digit (0-9) ddd Finds any three consecutive digits
w Letter, digit, or underscore w+ Finds a whole word
s Whitespace character s+ Finds one or more spaces
Important: In C++, strings with regular expressions need to be written with double escaping. For example, d in a regular expression becomes "\d" in C++ code. This is because the backslash has a special meaning in C++ strings.

Syntax in C++: all forms and variants

In C++, working with regular expressions is centered in the header file <regex>. The main classes and functions:

Class/Function Purpose Usage Example
std::regex Represents the regular expression itself std::regex pattern("\d+");
std::regex_match() Checks if the entire string matches the pattern regex_match("123", pattern)
std::regex_search() Searches for the first match in a string regex_search("abc123def", pattern)
std::regex_replace() Replaces matches with new text regex_replace("abc123", pattern, "X")