Skip to main content

Boost.Regex

boost::regex is a regular expression engine for C++ that supports Perl and POSIX syntax. It is the direct ancestor of std::regex — the standard committee adopted it essentially wholesale into C++11. Boost.Regex remains relevant because several standard library implementations of std::regex are notoriously slow, while Boost.Regex has had years of performance tuning.

The problem it solves

Before C++11, C++ had no built-in regular expression support at all. You either pulled in PCRE, wrote manual parsing loops, or shelled out to another language. Boost.Regex brought proper regex to C++ with a clean, iterator-friendly API that the standard later adopted.

Matching

boost::regex_match tests whether an entire string matches a pattern:

match.cpp
#include <boost/regex.hpp>
#include <iostream>
#include <string>

int main() {
boost::regex pattern(R"(\d{4}-\d{2}-\d{2})"); // YYYY-MM-DD
std::string date = "2026-04-07";

if (boost::regex_match(date, pattern)) {
std::cout << "valid date format\n";
}
}
regex_match versus regex_search

regex_match requires the entire string to match. regex_search finds a match within the string. Confusing the two is one of the most common regex bugs in C++.

Searching and capture groups

boost::regex_search finds the first match and populates a smatch (sub-match) object with capture groups:

search.cpp
#include <boost/regex.hpp>
#include <iostream>
#include <string>

int main() {
std::string log = "[ERROR] 2026-04-07 disk full on /dev/sda1";
boost::regex pattern(R"(\[(\w+)\]\s+(\d{4}-\d{2}-\d{2})\s+(.*))");
boost::smatch m;

if (boost::regex_search(log, m, pattern)) {
std::cout << "level: " << m[1] << "\n"; // ERROR
std::cout << "date: " << m[2] << "\n"; // 2026-04-07
std::cout << "msg: " << m[3] << "\n"; // disk full on /dev/sda1
}
}

Replacing

boost::regex_replace substitutes all matches with a replacement string. Back-references use $1, $2, etc.:

replace.cpp
#include <boost/regex.hpp>
#include <iostream>
#include <string>

int main() {
std::string text = "Call 555-1234 or 555-5678";
boost::regex phone(R"((\d{3})-(\d{4}))");

std::string redacted = boost::regex_replace(text, phone, "$1-XXXX");
std::cout << redacted << "\n";
// Call 555-XXXX or 555-XXXX
}

Iterating over all matches

boost::sregex_iterator walks through every match in a string, similar to std::sregex_iterator:

iterate.cpp
#include <boost/regex.hpp>
#include <iostream>
#include <string>

int main() {
std::string html = R"(<a href="one.html"> <a href="two.html">)";
boost::regex link(R"(href="([^"]+)")");

auto begin = boost::sregex_iterator(html.begin(), html.end(), link);
auto end = boost::sregex_iterator();

for (auto it = begin; it != end; ++it) {
std::cout << (*it)[1] << "\n";
}
// one.html
// two.html
}

Regex syntax flavours

Boost.Regex supports multiple syntax modes, selected at construction:

FlagSyntaxNotes
boost::regex::perl (default)Perl-compatiblemost common, supports look-ahead/behind
boost::regex::extendedPOSIX extendedegrep-like
boost::regex::basicPOSIX basicgrep-like, escapes reversed
boost::regex::icase(modifier)case-insensitive matching
boost::regex ci_pattern("hello", boost::regex::perl | boost::regex::icase);

Boost.Regex versus std::regex

Featureboost::regexstd::regex (C++11)
Header<boost/regex.hpp><regex>
Namespaceboost::std::
Performancewell-optimisedvaries by implementation (often slow)
Named capturesyes ((?P<name>...))implementation-defined
UnicodeICU integration availablelimited
Build requirementcompiled library (-lboost_regex)standard library
Which to choose

If your std::regex implementation is fast enough (measure it), prefer the standard. Otherwise Boost.Regex is a drop-in improvement — the API is nearly identical. On GCC's libstdc++, where std::regex has historically been slow, Boost.Regex can be an order of magnitude faster.

Boost.Regex is a compiled library

Unlike most Boost libraries, Boost.Regex is not header-only. You must link against -lboost_regex. See header-only vs compiled.

See also