liveBook · Manning

Pattern matching

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

Pattern matching is the process of determining whether structured data conforms to a specified pattern.

Pattern matching checks whether structured data follows a required form. Text parsing makes this practical: input may need validation or separation into meaningful parts. section 4.8

Handwritten checks can be laborious, imprecise, and difficult to maintain. A simple email validator may find separators and nonempty words while still accepting invalid characters or malformed input. section 4.8

Modern C++ provides regular expressions through the regex header. A regular expression describes allowed text, while matching is typically implemented through state transitions; the pattern is easier to revise than scattered parser conditions. section 4.8

Email validation

The handcoded version searches for an at sign and period, then checks that three words exist. The regex version expresses the intended username, domain, and top-level-domain structure in one pattern.

Listing 4.18 Handcoded email validation function (with bugs)
bool isValidEmail(const std::string& e) {
    size_t pos_at = e.find('@');                             #1
    if (pos_at == std::string::npos)  // no '@' found
        return false;
    if (pos_at == 0)  // no first word                       #2
        return false;
    size_t pos_period = e.find('.', pos_at);                 #3
    if (pos_period == std::string::npos)  // no '.' found
        return false;
    if (pos_at + 1 >= pos_period)  // no middle word         #4
        return false;
    if (pos_period == e.length() - 1)  // no last word       #5
        return false;
    return true;
}
 
int main() {
    std::cout << isValidEmail("prof@nu.edu") << '\n';
    return 0;
}

Inspect the validation conditions and the gaps that make handwritten pattern matching imprecise. section 4.8

Listing 4.19 Using a regular expression for email validation
bool isValidEmail(const std::string& e) {
    std::regex pattern(
        R"([a-zA-Z0-9._%+-][CA}+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})"; #1
    return std::regex_search(e, pattern);
}
  
int main() {
    std::cout << isValidEmail("prof@nu.edu") << '\n';
    return 0;
}

Compare the compact regular-expression implementation with the manually coordinated separator checks. section 4.8

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

How deep it goes
Contrasts handwritten checks with regular expressions and briefly connects matching to state machines, stopping short of parser construction.
Examples
Demonstrates email validation with separator checks and a regular expression.
What it assumes
Assumes basic string handling and teaches the motivation for regular expressions from a concrete validation problem.
What you can do after
Apply it via std::regex to replace fragile handwritten checks for structured textual input.
How it connects
Connects pattern matching with regular expressions, parsing, string validation, and state machines.

Also explained in

Compare how 12 books explain pattern matching →

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.