Boost C++ Libraries of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards


Function template check

boost::xpressive::check — For adding user-defined assertions to your regular expressions.


// In header: <boost/xpressive/regex_actions.hpp>

template<typename T> unspecified check(T const & t);


A user-defined assertion is a kind of semantic action that evaluates a Boolean lambda and, if it evaluates to false, causes the match to fail at that location in the string. This will cause backtracking, so the match may ultimately succeed.

To use check() to specify a user-defined assertion in a regex, use the following syntax:

sregex s = (_d >> _d)[check( XXX )]; // XXX is a custom assertion

The assertion is evaluated with a sub_match<> object that delineates what part of the string matched the sub-expression to which the assertion was attached.

check() can be used with an ordinary predicate that takes a sub_match<> object as follows:

// A predicate that is true IFF a sub-match is
// either 3 or 6 characters long.
struct three_or_six
    bool operator()(ssub_match const &sub) const
        return sub.length() == 3 || sub.length() == 6;

// match words of 3 characters or 6 characters.
sregex rx = (bow >> +_w >> eow)[ check(three_or_six()) ] ;

Alternately, check() can be used to define inline custom assertions with the same syntax as is used to define semantic actions. The following code is equivalent to above:

// match words of 3 characters or 6 characters.
sregex rx = (bow >> +_w >> eow)[ check(length(_)==3 || length(_)==6) ] ;

Within a custom assertion, _ is a placeholder for the sub_match<> That delineates the part of the string matched by the sub-expression to which the custom assertion was attached.



The UnaryPredicate object or Boolean semantic action.