Regular Expressions in C++

C++ provides regular expression support through the <regex> library. Here are some examples of common regex-related tasks in C++.

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

int main() {
    // This tests whether a pattern matches a string.
    std::regex pattern("p([a-z]+)ch");
    std::string text = "peach";
    bool match = std::regex_search(text, pattern);
    std::cout << std::boolalpha << match << std::endl;

    // Many methods are available on regex objects. Here's
    // a match test like we saw earlier.
    std::cout << std::regex_search("peach", pattern) << std::endl;

    // This finds the match for the regex.
    std::smatch sm;
    if (std::regex_search("peach punch", sm, pattern))
        std::cout << sm.str() << std::endl;

    // The Submatch variants include information about
    // both the whole-pattern matches and the submatches
    // within those matches.
    if (std::regex_search("peach punch", sm, pattern)) {
        std::cout << sm[0] << " " << sm[1] << std::endl;
    }

    // To find all matches for a regex, we can use std::regex_iterator
    std::string s = "peach punch pinch";
    std::regex_iterator<std::string::iterator> rit(s.begin(), s.end(), pattern);
    std::regex_iterator<std::string::iterator> rend;
    
    while (rit != rend) {
        std::cout << rit->str() << " ";
        ++rit;
    }
    std::cout << std::endl;

    // The regex package can also be used to replace
    // subsets of strings with other values.
    std::string result = std::regex_replace("a peach", pattern, "<fruit>");
    std::cout << result << std::endl;

    // The regex_replace function allows you to transform matched
    // text with a given function.
    result = std::regex_replace("a peach", pattern, [](const std::smatch& m) {
        std::string s = m.str();
        std::transform(s.begin(), s.end(), s.begin(), ::toupper);
        return s;
    });
    std::cout << result << std::endl;

    return 0;
}

To run the program, compile it and then execute:

$ g++ -std=c++11 regular_expressions.cpp -o regular_expressions
$ ./regular_expressions
true
true
peach
peach ea
peach punch pinch 
a <fruit>
a PEACH

C++’s <regex> library provides a powerful set of tools for working with regular expressions. The std::regex class represents a regular expression, and functions like std::regex_search, std::regex_match, and std::regex_replace allow you to perform various operations with these expressions.

Unlike in some other languages, C++ requires you to explicitly compile the regular expression (by creating a std::regex object) before you can use it. This can be more efficient if you’re going to use the same expression multiple times.

The std::smatch class (which stands for “string match”) is used to store the results of a regex operation on a string. It contains information about the overall match as well as any captured subexpressions.

C++ also provides std::regex_iterator for iterating over all matches in a string, which can be useful for more complex parsing tasks.

For a complete reference on C++ regular expressions, check the documentation for the <regex> library.