Site Tools


wiki:hpp-regex

Table of Contents

<regex>

<regex> provides regular expression matching via std::regex, std::regex_match, std::regex_search, and std::regex_replace. The syntax defaults to ECMAScript (JavaScript-like) regex.

C++ regex is slower than dedicated libraries like oniguruma or PCRE2, but is portable and does not require external dependencies.

Example

This example searches for a regex pattern in a string and performs a regex-based replacement to modify text.

// compile: g++ -std=c++11 -o regexexample regexexample.cpp
// run: ./regexexample
// description: regex matching and replacement
 
#include <regex>
#include <iostream>
#include <string>
 
int main() {
    std::string text = "hello world";
    std::regex pattern("l+o");
 
    if (std::regex_search(text, pattern)) {
        std::cout << "found match\n";
    }
 
    std::string result = std::regex_replace(text, pattern, "X");
    std::cout << "after replace: " << result << "\n";
 
    return 0;
}
wiki/hpp-regex.md · Last modified: by 127.0.0.1