Lesson 17-C++ Regular Expressions

Regular Expressions (Regex) are a powerful string-matching tool used to describe, search, and manipulate text that conforms to specific patterns. C++11 and later standards provide regular expression support through the <regex> header file. This article comprehensively analyzes the core functionality of C++ regular expressions, from basic concepts to practical applications.

Basic Concepts

Definition of Regular Expressions

A regular expression is a pattern composed of characters and special symbols that describes the structure or content of strings. By defining rules for character matching (such as combinations of letters, numbers, and symbols), it enables operations like searching, matching, and replacing target substrings in text.

Core Components of C++ Regular Expressions

  • std::regex: The regular expression object that stores the compiled pattern.
  • std::smatch/std::cmatch: Containers for match results (smatch is used with std::string, cmatch with C-style strings).
  • std::regex_match: Checks whether the entire string fully matches the regular expression pattern.
  • std::regex_search: Searches for the first matching substring within a string.
  • std::regex_replace: Replaces parts of a string that match the regular expression pattern.

Creating Regular Expressions

Constructing a std::regex Object

Use the std::regex constructor to create a regular expression object, with parameters being the pattern string and optional syntax flags.

Syntax Flags (std::regex_constants):

  • icase: Case-insensitive matching (e.g., [a-z] matches uppercase letters).
  • dotall: The dot . matches newline characters (by default, it does not).
  • multiline: ^ matches the start of a line, $ matches the end of a line (by default, they match the start/end of the entire string).
  • optimize: Optimizes the regular expression (improves matching performance).

Example: Creating a Regex Object

#include <regex>
#include <iostream>

int main() {
    // Regular expression to match digits (case-insensitive, irrelevant here)
    std::regex num_regex(R"(\d+)");  // R"()" is a raw string literal to avoid escape issues

    // Regex with options (matches URLs starting with "http://" or "https://")
    std::regex url_regex(
        R"(^(https?)://[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}(/\S*)?$)",
        std::regex_constants::icase  // Ignore case in protocol (e.g., HTTP://)
    );

    return 0;
}

Pattern Matching

Full Match (std::regex_match)

Checks whether the entire string completely conforms to the regex pattern.

Example: Validating an Integer

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

bool is_integer(const std::string& str) {
    // Matches integers (optional sign + digits)
    std::regex int_regex(R"(^[+-]?\d+)$");
    return std::regex_match(str, int_regex);
}

int main() {
    std::cout << std::boolalpha;
    std::cout << is_integer("123") << std::endl;   // true
    std::cout << is_integer("-456") << std::endl;  // true
    std::cout << is_integer("12.3") << std::endl;  // false (contains decimal point)
    return 0;
}

Searches for the first matching substring in a string and returns whether it was found along with match details.

Example: Extracting Numbers from a String

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

void extract_numbers(const std::string& str) {
    std::regex num_regex(R"(\d+)");  // Match consecutive digits
    std::sregex_iterator it(str.begin(), str.end(), num_regex);
    std::sregex_iterator end;

    std::cout << "Found " << std::distance(it, end) << " numbers:\n";
    for (; it != end; ++it) {
        std::smatch match = *it;
        std::cout << "  " << match.str() << std::endl;
    }
}

int main() {
    std::string text = "Order ID: 12345, Amount: 678.90 yuan";
    extract_numbers(text);  // Output: 12345, 678, 90 (note decimal point splits 678.90)
    return 0;
}

Grouping and Capturing

Capturing Groups

Use parentheses () to define capturing groups, which extract subpatterns from a match. Results are stored in std::smatch as sub_match objects, indexed starting from 1 (\1 refers to the first capture group).

Example: Extracting Year, Month, Day from a Date

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

void parse_date(const std::string& date_str) {
    // Regex: year(4 digits)-month(2 digits)-day(2 digits)
    std::regex date_regex(R"((\d{4})-(\d{2})-(\d{2}))");
    std::smatch match;

    if (std::regex_match(date_str, match, date_regex)) {
        std::cout << "Year: " << match[1].str() << std::endl;  // 1st capture group (year)
        std::cout << "Month: " << match[2].str() << std::endl;  // 2nd capture group (month)
        std::cout << "Day: " << match[3].str() << std::endl;   // 3rd capture group (day)
    } else {
        std::cout << "Invalid date format" << std::endl;
    }
}

int main() {
    parse_date("2023-10-01");  // Output: Year: 2023, Month: 10, Day: 01
    return 0;
}

Non-Capturing Groups

Use (?:pattern) to define non-capturing groups, which are used only for matching and do not store results, saving memory.

Example: Matching Repeated Words (Non-Capturing Group)

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

void find_repeated_words(const std::string& text) {
    // Match repeated words (e.g., "the the")
    // Non-capturing group (?:\w+) matches a word but does not capture it
    std::regex repeat_regex(R"(\b(\w+)(?:\s+\1)\b)");
    std::sregex_iterator it(text.begin(), text.end(), repeat_regex);
    std::sregex_iterator end;

    std::cout << "Found repeated words:\n";
    for (; it != end; ++it) {
        std::smatch match = *it;
        std::cout << "  " << match[1].str() << std::endl;  // Only capture the first word
    }
}

int main() {
    std::string text = "hello hello world world world";
    find_repeated_words(text);  // Output: hello, world
    return 0;
}

Backreferences

Use \n (where n is the capture group number) in the regex to refer to previously captured content, useful for matching repeated patterns.

Example: Matching Palindromes (Simple Version)

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

void find_palindrome(const std::string& text) {
    // Match palindromes like "aa" or "aba" (simple example)
    std::regex palindrome_regex(R"((\w)(\w)?\2\1)");
    std::sregex_iterator it(text.begin(), text.end(), palindrome_regex);
    std::sregex_iterator end;

    std::cout << "Found palindromes:\n";
    for (; it != end; ++it) {
        std::smatch match = *it;
        std::cout << "  " << match.str() << std::endl;
    }
}

int main() {
    std::string text = "aa aba abba test";
    find_palindrome(text);  // Output: aa, aba, abba
    return 0;
}

Modifiers (Syntax Options)

Common Modifiers

ModifierDescription
icaseCase-insensitive (e.g., [a-z] matches A-Z).
dotallThe dot . matches newline characters (by default, it does not).
multiline^ matches start of line, $ matches end of line (by default, they match the start/end of the entire string).
optimizeOptimizes the regular expression (improves matching efficiency, may increase compile time).
collateEnables locale-specific character comparison (e.g., a and á are treated differently).

Example: Using dotall to Match Multiline Text

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

void match_multiline(const std::string& text) {
    // Match content wrapped in parentheses across multiple lines (using dotall)
    std::regex multiline_regex(
        R"(^\s*\((.*)\)\s*$)",
        std::regex_constants::dotall | std::regex_constants::multiline
    );
    std::smatch match;

    if (std::regex_search(text, match, multiline_regex)) {
        std::cout << "Content inside parentheses:\n" << match[1].str() << std::endl;
    }
}

int main() {
    std::string text = "(This is a\nmultiline\ntext)";
    match_multiline(text);  // Output: all content inside parentheses (including newlines)
    return 0;
}

Repetition Qualifiers

Repetition qualifiers specify how many times a pattern should match. Common forms include:

QualifierDescriptionExample
*0 or more timesa* matches “”, “a”, “aa”
+1 or more timesa+ matches “a”, “aa”
?0 or 1 timea? matches “”, “a”
{n}Exactly n timesa{3} matches “aaa”
{n,}At least n timesa{3,} matches “aaa”, “aaaa”
{n,m}Between n and m times (inclusive)a{2,4} matches “aa”, “aaa”, “aaaa”

Example: Matching a Phone Number (Simplified)

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

bool is_phone_number(const std::string& phone) {
    // Match 11-digit number starting with 1
    std::regex phone_regex(R"(^1\d{10}$)");
    return std::regex_match(phone, phone_regex);
}

int main() {
    std::cout << std::boolalpha;
    std::cout << is_phone_number("13812345678") << std::endl;  // true
    std::cout << is_phone_number("1234567890") << std::endl;   // false (insufficient length)
    return 0;
}

Lookaround and Backtracking

Lookaround Assertions

Lookaround checks whether preceding or following characters satisfy a condition during matching, but does not consume characters. Includes positive lookahead (?=pattern) and negative lookahead (?!pattern).

Example: Extracting Prices (Excluding Non-Digit Characters After Currency)

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

void extract_prices(const std::string& text) {
    // Match prices (e.g., "$199.99" or "¥299")
    // Positive lookahead (?=[$¥]) ensures a currency symbol precedes
    // Negative lookahead (?!\d) ensures no digit follows (to avoid matching multiple decimal places)
    std::regex price_regex(
        R"((?=[\$¥])(\$|¥)?(\d+)(?:\.\d+)?)"
    );
    std::sregex_iterator it(text.begin(), text.end(), price_regex);
    std::sregex_iterator end;

    std::cout << "Found prices:\n";
    for (; it != end; ++it) {
        std::smatch match = *it;
        std::cout << "  Symbol: " << (match[1].matched ? match[1].str() : "None")
                  << " Amount: " << match[2].str() << std::endl;
    }
}

int main() {
    std::string text = "Product A price $199.99, Product B price ¥299";
    extract_prices(text);  // Output: $199.99, ¥299
    return 0;
}

Backtracking

When matching fails, the regex engine tries alternative paths (backtracking), which can cause performance issues (especially with long text or complex patterns). Unnecessary backtracking should be avoided.

Example: Backtracking Due to Greedy Matching

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

void greedy_vs_lazy() {
    std::string text = "a1b2c3d4e5";

    // Greedy matching (.* matches as much as possible)
    std::regex greedy_regex(R"(a.*e)");
    std::smatch greedy_match;
    if (std::regex_search(text, greedy_match, greedy_regex)) {
        std::cout << "Greedy match result: " << greedy_match.str() << std::endl;  // a1b2c3d4e
    }

    // Lazy matching (.*? matches as little as possible)
    std::regex lazy_regex(R"(a.*?e)");
    std::smatch lazy_match;
    if (std::regex_search(text, lazy_match, lazy_regex)) {
        std::cout << "Lazy match result: " << lazy_match.str() << std::endl;  // a1b2c3d4e (same here due to single 'e')
    }

    // More complex example: match shortest "a" followed by digits
    text = "a1a2a3";
    std::regex complex_greedy(R"(a\d+)");  // Greedy matches longest "a1a2a3"
    std::regex complex_lazy(R"(a\d+?)");   // Lazy matches shortest "a1"
    std::smatch cg_match, cl_match;
    std::regex_search(text, cg_match, complex_greedy);
    std::regex_search(text, cl_match, complex_lazy);
    std::cout << "Complex greedy match: " << cg_match.str() << std::endl;  // a1a2a3
    std::cout << "Complex lazy match: " << cl_match.str() << std::endl;    // a1
}

int main() {
    greedy_vs_lazy();
    return 0;
}

Escaping Special Characters

Special characters in regex (such as ., *, +, ?, ^, $, (, ), [, ], {, }, |, \) must be escaped with a backslash \ if used literally; otherwise, they are treated as metacharacters.

Example: Matching Special Characters

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

void match_special_chars(const std::string& text) {
    // Match strings containing "." and "*"
    std::regex special_regex(R"(\.\*)");  // Escape . and *
    std::smatch match;

    if (std::regex_search(text, match, special_regex)) {
        std::cout << "Found special character sequence: " << match.str() << std::endl;
    }
}

int main() {
    std::string text = "File path: C:\\Program Files\\*.txt";
    match_special_chars(text);  // Output: \.* (matches literal ".*")
    return 0;
}

Practical Case: Extracting URLs

Requirements Analysis

Extract all URLs with http:// or https:// protocol from text, in the format protocol://domain/path (e.g., https://www.example.com/path?query=1).

Regular Expression Design

  • Protocol part: https? (matches http or https).
  • Domain part: [a-zA-Z0-9.-]+\.[a-zA-Z]{2,} (matches letters, digits, dots, hyphens; top-level domain at least 2 characters).
  • Path part: (/\S*)? (optional, matches non-whitespace after /).

Final regex:
R"(https?://[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}(/\S*)?)"

Code Implementation

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

std::vector<std::string> extract_urls(const std::string& text) {
    std::vector<std::string> urls;
    // Regex to match URLs (case-insensitive)
    std::regex url_regex(
        R"(https?://[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}(/\S*)?)",
        std::regex_constants::icase
    );
    std::sregex_iterator it(text.begin(), text.end(), url_regex);
    std::sregex_iterator end;

    for (; it != end; ++it) {
        urls.push_back(it->str());
    }
    return urls;
}

int main() {
    std::string text = R"(
        Visit my website: https://www.example.com,
        Download file: http://download.example.org/path/to/file.zip,
        Invalid link: ftp://example.com (non-HTTP protocol)
    )";

    std::vector<std::string> urls = extract_urls(text);
    std::cout << "Extracted URLs:\n";
    for (const auto& url : urls) {
        std::cout << "  " << url << std::endl;
    }
    return 0;
}

Output Result:

Extracted URLs:
  https://www.example.com
  http://download.example.org/path/to/file.zip

Summary and Practice

Key Summary

  • Regular expressions are a powerful tool in C++ for string processing, suitable for matching, searching, replacing, etc.
  • std::regex is introduced in C++11 and supports ECMAScript syntax (adjustable via flags).
  • Grouping and capturing extract subpatterns; backreferences match repeated content.
  • Modifiers (e.g., icase, dotall) adjust matching rules; lookaround enables complex conditional checks.
  • Greedy vs. lazy matching should be chosen based on the scenario to avoid performance issues from unnecessary backtracking.

Practice

  • Precompile regex: For repeated use, declare std::regex as const or static to avoid recompilation.
  • Escape special characters: Use raw string literals (R"()") to reduce escape confusion.
  • Test regex: Use online tools like RegExr to validate patterns and avoid logic errors.
  • Performance optimization: For long text or high-frequency matching, use std::regex_constants::optimize.

By mastering the core functionality of C++ regular expressions, developers can efficiently handle string matching, data cleaning, log analysis, and more, enhancing code robustness and maintainability.

Share your love