liveBook · Manning

Regular expression

As explained in 100 C++ Mistakes and How to Avoid Them

A regular expression is a formal pattern specification for recognizing, extracting, or substituting portions of text.

A regular expression specifies a text pattern for recognizing, extracting, or substituting text. In modern C++, it offers a safer alternative to hand-written parsing, whose conditions become difficult to extend and whose comments can become stale. section 4.0 section 4.8

An email-validation pattern proceeds from left to right:

  • Match a username containing permitted letters, digits, and limited punctuation. section 4.8
  • Match the at-sign separator and a domain containing letters or digits. section 4.8
  • Match an escaped period followed by a top-level domain containing at least two letters. section 4.8

Replacing brittle email checks

A hand-coded validator checks for an at sign, a period, and nonempty sections, but admits invalid characters. A std::regex pattern used with std::regex_search expresses the permitted structure more precisely. section 4.8

Making patterns readable

A raw string such as R"(\\)" avoids the extra escaping required by an ordinary C++ string. The regular expression still needs its own escape when a character, such as a period, has special meaning. section 4.8

How 100 C++ Mistakes and How to Avoid Them covers it

How deep it goes
Defines state-machine matching and explains pattern components, raw strings, and email validation, without detailing broader regex syntax.
Examples
Uses a buggy hand-coded email validator, a std::regex email pattern, and ordinary versus raw-string backslash representations.
What it assumes
Assumes basic C++ strings and conditions while teaching pattern structure and raw-string escaping directly.
What you can do after
Apply std::regex patterns to validate structured text and reduce maintenance-prone parsing code.
How it connects
Connects regular expressions with text parsing, state machines, raw strings, and email validation.

Also explained in

Compare how 27 books explain regular expression →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 C++ Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.