Chapter 9. Perl-compatible regular expressions

#include <x/pcre.H>

std::string line;

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

if (!pattern->match(line))
    std::cout << "No match" < < std::endl;
    if (pattern->subpatterns.size() == 3)

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

x::pcre constructs 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 int of option flags, see pcre_compile(3) for a list of available option flags.

Once compiled, match() checks if the string matches a regular expression. match() also takes an optional second argument, an int of option flags, pcre_exec(3) for a list of available option flags.

Upon a succesful match, the part of the string that matched each subpattern, if any, in the regular expression can be found in the subpatterns vector. subpatterns[0] is always the entire matched string, subpatterns[1] is the first subpattern, and so on.


Although, once constructed, the reference-counted object's match() can be invoked by multiple threads, this object is not directly thread safe. It's up to you to make the necessary arrangement for invoking match(), and accessing subpatterns by one thread at a time.


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