1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 2<html> 3<head> 4<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 5<title>Function template check</title> 6<link rel="stylesheet" href="../../../../doc/src/boostbook.css" type="text/css"> 7<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 8<link rel="home" href="../../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset"> 9<link rel="up" href="../../xpressive/reference.html#header.boost.xpressive.regex_actions_hpp" title="Header <boost/xpressive/regex_actions.hpp>"> 10<link rel="prev" href="cref.html" title="Function template cref"> 11<link rel="next" href="let.html" title="Function template let"> 12</head> 13<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 14<table cellpadding="2" width="100%"><tr> 15<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../boost.png"></td> 16<td align="center"><a href="../../../../index.html">Home</a></td> 17<td align="center"><a href="../../../../libs/libraries.htm">Libraries</a></td> 18<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 19<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 20<td align="center"><a href="../../../../more/index.htm">More</a></td> 21</tr></table> 22<hr> 23<div class="spirit-nav"> 24<a accesskey="p" href="cref.html"><img src="../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../../xpressive/reference.html#header.boost.xpressive.regex_actions_hpp"><img src="../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="let.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a> 25</div> 26<div class="refentry"> 27<a name="boost.xpressive.check"></a><div class="titlepage"></div> 28<div class="refnamediv"> 29<h2><span class="refentrytitle">Function template check</span></h2> 30<p>boost::xpressive::check — For adding user-defined assertions to your regular expressions. </p> 31</div> 32<h2 xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="refsynopsisdiv-title">Synopsis</h2> 33<div xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="refsynopsisdiv"><pre class="synopsis"><span class="comment">// In header: <<a class="link" href="../../xpressive/reference.html#header.boost.xpressive.regex_actions_hpp" title="Header <boost/xpressive/regex_actions.hpp>">boost/xpressive/regex_actions.hpp</a>> 34 35</span> 36<span class="keyword">template</span><span class="special"><</span><span class="keyword">typename</span> T<span class="special">></span> <span class="emphasis"><em><span class="identifier">unspecified</span></em></span> <span class="identifier">check</span><span class="special">(</span><span class="identifier">T</span> <span class="keyword">const</span> <span class="special">&</span> t<span class="special">)</span><span class="special">;</span></pre></div> 37<div class="refsect1"> 38<a name="id-1.3.47.5.4.48.4"></a><h2>Description</h2> 39<p> 40A <a class="link" href="../../xpressive/user_s_guide.html#boost_xpressive.user_s_guide.semantic_actions_and_user_defined_assertions.user_defined_assertions">user-defined assertion</a> 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.</p> 41<p>To use <code class="computeroutput">check()</code> to specify a user-defined assertion in a regex, use the following syntax:</p> 42<pre class="programlisting"><span class="identifier">sregex</span> <span class="identifier">s</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">_d</span> <span class="special">>></span> <span class="identifier">_d</span><span class="special">)</span><span class="special">[</span><span class="identifier">check</span><span class="special">(</span> <span class="identifier">XXX</span> <span class="special">)</span><span class="special">]</span><span class="special">;</span> <span class="comment">// XXX is a custom assertion</span> 43</pre> 44<p>The assertion is evaluated with a <code class="computeroutput"><code class="computeroutput"><a class="link" href="sub_match.html" title="Struct template sub_match">sub_match</a></code><></code> object that delineates what part of the string matched the sub-expression to which the assertion was attached.</p> 45<p><code class="computeroutput">check()</code> can be used with an ordinary predicate that takes a <code class="computeroutput"><code class="computeroutput"><a class="link" href="sub_match.html" title="Struct template sub_match">sub_match</a></code><></code> object as follows:</p> 46<pre class="programlisting"><span class="comment">// A predicate that is true IFF a sub-match is</span> 47<span class="comment">// either 3 or 6 characters long.</span> 48<span class="keyword">struct</span> <span class="identifier">three_or_six</span> 49<span class="special">{</span> 50 <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">(</span><span class="special">)</span><span class="special">(</span><span class="identifier">ssub_match</span> <span class="keyword">const</span> <span class="special">&</span><span class="identifier">sub</span><span class="special">)</span> <span class="keyword">const</span> 51 <span class="special">{</span> 52 <span class="keyword">return</span> <span class="identifier">sub</span><span class="special">.</span><span class="identifier">length</span><span class="special">(</span><span class="special">)</span> <span class="special">==</span> <span class="number">3</span> <span class="special">||</span> <span class="identifier">sub</span><span class="special">.</span><span class="identifier">length</span><span class="special">(</span><span class="special">)</span> <span class="special">==</span> <span class="number">6</span><span class="special">;</span> 53 <span class="special">}</span> 54<span class="special">}</span><span class="special">;</span> 55 56<span class="comment">// match words of 3 characters or 6 characters.</span> 57<span class="identifier">sregex</span> <span class="identifier">rx</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">bow</span> <span class="special">>></span> <span class="special">+</span><span class="identifier">_w</span> <span class="special">>></span> <span class="identifier">eow</span><span class="special">)</span><span class="special">[</span> <span class="identifier">check</span><span class="special">(</span><span class="identifier">three_or_six</span><span class="special">(</span><span class="special">)</span><span class="special">)</span> <span class="special">]</span> <span class="special">;</span> 58</pre> 59<p>Alternately, <code class="computeroutput">check()</code> 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:</p> 60<pre class="programlisting"><span class="comment">// match words of 3 characters or 6 characters.</span> 61<span class="identifier">sregex</span> <span class="identifier">rx</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">bow</span> <span class="special">>></span> <span class="special">+</span><span class="identifier">_w</span> <span class="special">>></span> <span class="identifier">eow</span><span class="special">)</span><span class="special">[</span> <span class="identifier">check</span><span class="special">(</span><span class="identifier">length</span><span class="special">(</span><span class="identifier">_</span><span class="special">)</span><span class="special">==</span><span class="number">3</span> <span class="special">||</span> <span class="identifier">length</span><span class="special">(</span><span class="identifier">_</span><span class="special">)</span><span class="special">==</span><span class="number">6</span><span class="special">)</span> <span class="special">]</span> <span class="special">;</span> 62</pre> 63<p>Within a custom assertion, <code class="computeroutput">_</code> is a placeholder for the <code class="computeroutput"><code class="computeroutput"><a class="link" href="sub_match.html" title="Struct template sub_match">sub_match</a></code><></code> That delineates the part of the string matched by the sub-expression to which the custom assertion was attached. </p> 64<div class="variablelist"><table border="0" class="variablelist compact"> 65<colgroup> 66<col align="left" valign="top"> 67<col> 68</colgroup> 69<tbody><tr> 70<td><p><span class="term">Parameters:</span></p></td> 71<td><div class="variablelist"><table border="0" class="variablelist compact"> 72<colgroup> 73<col align="left" valign="top"> 74<col> 75</colgroup> 76<tbody><tr> 77<td><p><span class="term"><code class="computeroutput">t</code></span></p></td> 78<td><p>The UnaryPredicate object or Boolean semantic action.</p></td> 79</tr></tbody> 80</table></div></td> 81</tr></tbody> 82</table></div> 83</div> 84</div> 85<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 86<td align="left"></td> 87<td align="right"><div class="copyright-footer">Copyright © 2007 Eric Niebler<p> 88 Distributed under the Boost Software License, Version 1.0. (See accompanying 89 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 90 </p> 91</div></td> 92</tr></table> 93<hr> 94<div class="spirit-nav"> 95<a accesskey="p" href="cref.html"><img src="../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../../xpressive/reference.html#header.boost.xpressive.regex_actions_hpp"><img src="../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="let.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a> 96</div> 97</body> 98</html> 99