Chapter 9. Perl-compatible regular expressions

#include <x/pcre.H>

std::string line;

x::pcre pattern=x::pcre::create("(.*) (.*)");

std::vector<std::string_view> patterns=pattern->match("abra cadabra");

if (patterns.size() == 3)
     process_words(patterns[1],
                   patterns[2]);

This is a Perl-compatible regular expression engine, a facade for the PCRE2 library.

x::pcre is a reference to a reference-counted object that specifies a regular expression. The create() constructor throws an exception if the given regular expression's syntax is malformed. create() takes an optional second argument, an uint32_t of option flags, see pcre2_compile(3) for a list of available option flags.

Once compiled, match() checks if the string matches a regular expression. match() takes two optional arguments:

  1. Starting index to search, this defaults to 0.

  2. Options for pcre2_match(3).

An empty vector gets returned if the match fails. A successful regular expression match returns a non-empty vector of std::string_view. The first one is the matched regular expression. Sub-patterns (as specified by the regular expression) get returned as additional values.

Note

The search string parameter must be an lvalue, and cannot be a temporary rvalue. The string views are views into the exact location in the original string. As such, each string view's data() gives the exact location in the passed-in string where the regular expression was matched.

std::vector<std::vector<std::string_view>> patterns=pattern->match_all("abra cadabra");

There's also a match_all() that returns a vector of std::string_view vectors. match_all() repeatedly invokes match() to find every occurence of the regular expression in the searched string.

Note

An error from the underlying PCRE library can result in a thrown exception. The caller should catch any thrown exceptions, and decide what to do with them.