boost-react-native-bundle
Version:
Boost library as in https://sourceforge.net/projects/boost/files/boost/1.57.0/
856 lines (850 loc) • 465 kB
HTML
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>User's Guide</title>
<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.78.1">
<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
<link rel="up" href="../xpressive.html" title="Chapter 39. Boost.Xpressive">
<link rel="prev" href="../xpressive.html" title="Chapter 39. Boost.Xpressive">
<link rel="next" href="reference.html" title="Reference">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
<td align="center"><a href="../../../index.html">Home</a></td>
<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../xpressive.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../xpressive.html"><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="reference.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="xpressive.user_s_guide"></a><a class="link" href="user_s_guide.html" title="User's Guide">User's Guide</a>
</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="user_s_guide.html#boost_xpressive.user_s_guide.introduction">Introduction</a></span></dt>
<dt><span class="section"><a href="user_s_guide.html#boost_xpressive.user_s_guide.installing_xpressive">Installing
xpressive</a></span></dt>
<dt><span class="section"><a href="user_s_guide.html#boost_xpressive.user_s_guide.quick_start">Quick Start</a></span></dt>
<dt><span class="section"><a href="user_s_guide.html#xpressive.user_s_guide.creating_a_regex_object">Creating
a Regex Object</a></span></dt>
<dt><span class="section"><a href="user_s_guide.html#boost_xpressive.user_s_guide.matching_and_searching">Matching
and Searching</a></span></dt>
<dt><span class="section"><a href="user_s_guide.html#boost_xpressive.user_s_guide.accessing_results">Accessing
Results</a></span></dt>
<dt><span class="section"><a href="user_s_guide.html#boost_xpressive.user_s_guide.string_substitutions">String
Substitutions</a></span></dt>
<dt><span class="section"><a href="user_s_guide.html#boost_xpressive.user_s_guide.string_splitting_and_tokenization">String
Splitting and Tokenization</a></span></dt>
<dt><span class="section"><a href="user_s_guide.html#boost_xpressive.user_s_guide.named_captures">Named Captures</a></span></dt>
<dt><span class="section"><a href="user_s_guide.html#boost_xpressive.user_s_guide.grammars_and_nested_matches">Grammars
and Nested Matches</a></span></dt>
<dt><span class="section"><a href="user_s_guide.html#boost_xpressive.user_s_guide.semantic_actions_and_user_defined_assertions">Semantic
Actions and User-Defined Assertions</a></span></dt>
<dt><span class="section"><a href="user_s_guide.html#boost_xpressive.user_s_guide.symbol_tables_and_attributes">Symbol
Tables and Attributes</a></span></dt>
<dt><span class="section"><a href="user_s_guide.html#boost_xpressive.user_s_guide.localization_and_regex_traits">Localization
and Regex Traits</a></span></dt>
<dt><span class="section"><a href="user_s_guide.html#boost_xpressive.user_s_guide.tips_n_tricks">Tips 'N Tricks</a></span></dt>
<dt><span class="section"><a href="user_s_guide.html#boost_xpressive.user_s_guide.concepts">Concepts</a></span></dt>
<dt><span class="section"><a href="user_s_guide.html#boost_xpressive.user_s_guide.examples">Examples</a></span></dt>
</dl></div>
<p>
This section describes how to use xpressive to accomplish text manipulation
and parsing tasks. If you are looking for detailed information regarding specific
components in xpressive, check the <a class="link" href="reference.html" title="Reference">Reference</a>
section.
</p>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_xpressive.user_s_guide.introduction"></a><a class="link" href="user_s_guide.html#boost_xpressive.user_s_guide.introduction" title="Introduction">Introduction</a>
</h3></div></div></div>
<h3>
<a name="boost_xpressive.user_s_guide.introduction.h0"></a>
<span class="phrase"><a name="boost_xpressive.user_s_guide.introduction.what_is_xpressive_"></a></span><a class="link" href="user_s_guide.html#boost_xpressive.user_s_guide.introduction.what_is_xpressive_">What
is xpressive?</a>
</h3>
<p>
xpressive is a regular expression template library. Regular expressions (regexes)
can be written as strings that are parsed dynamically at runtime (dynamic
regexes), or as <span class="emphasis"><em>expression templates</em></span><a href="#ftn.boost_xpressive.user_s_guide.introduction.f0" class="footnote" name="boost_xpressive.user_s_guide.introduction.f0"><sup class="footnote">[9]</sup></a> that are parsed at compile-time (static regexes). Dynamic regexes
have the advantage that they can be accepted from the user as input at runtime
or read from an initialization file. Static regexes have several advantages.
Since they are C++ expressions instead of strings, they can be syntax-checked
at compile-time. Also, they can naturally refer to code and data elsewhere
in your program, giving you the ability to call back into your code from
within a regex match. Finally, since they are statically bound, the compiler
can generate faster code for static regexes.
</p>
<p>
xpressive's dual nature is unique and powerful. Static xpressive is a bit
like the <a href="http://spirit.sourceforge.net" target="_top">Spirit Parser Framework</a>.
Like <a href="http://spirit.sourceforge.net" target="_top">Spirit</a>, you can build
grammars with static regexes using expression templates. (Unlike <a href="http://spirit.sourceforge.net" target="_top">Spirit</a>,
xpressive does exhaustive backtracking, trying every possibility to find
a match for your pattern.) Dynamic xpressive is a bit like <a href="../../../libs/regex" target="_top">Boost.Regex</a>.
In fact, xpressive's interface should be familiar to anyone who has used
<a href="../../../libs/regex" target="_top">Boost.Regex</a>. xpressive's innovation
comes from allowing you to mix and match static and dynamic regexes in the
same program, and even in the same expression! You can embed a dynamic regex
in a static regex, or <span class="emphasis"><em>vice versa</em></span>, and the embedded regex
will participate fully in the search, back-tracking as needed to make the
match succeed.
</p>
<h3>
<a name="boost_xpressive.user_s_guide.introduction.h1"></a>
<span class="phrase"><a name="boost_xpressive.user_s_guide.introduction.hello__world_"></a></span><a class="link" href="user_s_guide.html#boost_xpressive.user_s_guide.introduction.hello__world_">Hello,
world!</a>
</h3>
<p>
Enough theory. Let's have a look at <span class="emphasis"><em>Hello World</em></span>, xpressive
style:
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iostream</span><span class="special">></span>
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">xpressive</span><span class="special">/</span><span class="identifier">xpressive</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">xpressive</span><span class="special">;</span>
<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
<span class="special">{</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">hello</span><span class="special">(</span> <span class="string">"hello world!"</span> <span class="special">);</span>
<span class="identifier">sregex</span> <span class="identifier">rex</span> <span class="special">=</span> <span class="identifier">sregex</span><span class="special">::</span><span class="identifier">compile</span><span class="special">(</span> <span class="string">"(\\w+) (\\w+)!"</span> <span class="special">);</span>
<span class="identifier">smatch</span> <span class="identifier">what</span><span class="special">;</span>
<span class="keyword">if</span><span class="special">(</span> <span class="identifier">regex_match</span><span class="special">(</span> <span class="identifier">hello</span><span class="special">,</span> <span class="identifier">what</span><span class="special">,</span> <span class="identifier">rex</span> <span class="special">)</span> <span class="special">)</span>
<span class="special">{</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">what</span><span class="special">[</span><span class="number">0</span><span class="special">]</span> <span class="special"><<</span> <span class="char">'\n'</span><span class="special">;</span> <span class="comment">// whole match</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">what</span><span class="special">[</span><span class="number">1</span><span class="special">]</span> <span class="special"><<</span> <span class="char">'\n'</span><span class="special">;</span> <span class="comment">// first capture</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">what</span><span class="special">[</span><span class="number">2</span><span class="special">]</span> <span class="special"><<</span> <span class="char">'\n'</span><span class="special">;</span> <span class="comment">// second capture</span>
<span class="special">}</span>
<span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
This program outputs the following:
</p>
<pre class="programlisting">hello world!
hello
world
</pre>
<p>
The first thing you'll notice about the code is that all the types in xpressive
live in the <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">xpressive</span></code> namespace.
</p>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
Most of the rest of the examples in this document will leave off the <code class="computeroutput"><span class="keyword">using</span> <span class="keyword">namespace</span>
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">xpressive</span><span class="special">;</span></code>
directive. Just pretend it's there.
</p></td></tr>
</table></div>
<p>
Next, you'll notice the type of the regular expression object is <code class="computeroutput"><span class="identifier">sregex</span></code>. If you are familiar with <a href="../../../libs/regex" target="_top">Boost.Regex</a>, this is different than what you
are used to. The "<code class="computeroutput"><span class="identifier">s</span></code>"
in "<code class="computeroutput"><span class="identifier">sregex</span></code>" stands
for "<code class="computeroutput"><span class="identifier">string</span></code>", indicating
that this regex can be used to find patterns in <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span></code>
objects. I'll discuss this difference and its implications in detail later.
</p>
<p>
Notice how the regex object is initialized:
</p>
<pre class="programlisting"><span class="identifier">sregex</span> <span class="identifier">rex</span> <span class="special">=</span> <span class="identifier">sregex</span><span class="special">::</span><span class="identifier">compile</span><span class="special">(</span> <span class="string">"(\\w+) (\\w+)!"</span> <span class="special">);</span>
</pre>
<p>
To create a regular expression object from a string, you must call a factory
method such as <code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/basic_regex.html#idp356796288-bb">basic_regex<>::compile()</a></code></code>.
This is another area in which xpressive differs from other object-oriented
regular expression libraries. Other libraries encourage you to think of a
regular expression as a kind of string on steroids. In xpressive, regular
expressions are not strings; they are little programs in a domain-specific
language. Strings are only one <span class="emphasis"><em>representation</em></span> of that
language. Another representation is an expression template. For example,
the above line of code is equivalent to the following:
</p>
<pre class="programlisting"><span class="identifier">sregex</span> <span class="identifier">rex</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">s1</span><span class="special">=</span> <span class="special">+</span><span class="identifier">_w</span><span class="special">)</span> <span class="special">>></span> <span class="char">' '</span> <span class="special">>></span> <span class="special">(</span><span class="identifier">s2</span><span class="special">=</span> <span class="special">+</span><span class="identifier">_w</span><span class="special">)</span> <span class="special">>></span> <span class="char">'!'</span><span class="special">;</span>
</pre>
<p>
This describes the same regular expression, except it uses the domain-specific
embedded language defined by static xpressive.
</p>
<p>
As you can see, static regexes have a syntax that is noticeably different
than standard Perl syntax. That is because we are constrained by C++'s syntax.
The biggest difference is the use of <code class="computeroutput"><span class="special">>></span></code>
to mean "followed by". For instance, in Perl you can just put sub-expressions
next to each other:
</p>
<pre class="programlisting"><span class="identifier">abc</span>
</pre>
<p>
But in C++, there must be an operator separating sub-expressions:
</p>
<pre class="programlisting"><span class="identifier">a</span> <span class="special">>></span> <span class="identifier">b</span> <span class="special">>></span> <span class="identifier">c</span>
</pre>
<p>
In Perl, parentheses <code class="computeroutput"><span class="special">()</span></code> have
special meaning. They group, but as a side-effect they also create back-references
like <code class="literal">$1</code> and <code class="literal">$2</code>. In C++, there is no
way to overload parentheses to give them side-effects. To get the same effect,
we use the special <code class="computeroutput"><span class="identifier">s1</span></code>, <code class="computeroutput"><span class="identifier">s2</span></code>, etc. tokens. Assign to one to create
a back-reference (known as a sub-match in xpressive).
</p>
<p>
You'll also notice that the one-or-more repetition operator <code class="computeroutput"><span class="special">+</span></code> has moved from postfix to prefix position.
That's because C++ doesn't have a postfix <code class="computeroutput"><span class="special">+</span></code>
operator. So:
</p>
<pre class="programlisting"><span class="string">"\\w+"</span>
</pre>
<p>
is the same as:
</p>
<pre class="programlisting"><span class="special">+</span><span class="identifier">_w</span>
</pre>
<p>
We'll cover all the other differences <a class="link" href="user_s_guide.html#boost_xpressive.user_s_guide.creating_a_regex_object.static_regexes" title="Static Regexes">later</a>.
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_xpressive.user_s_guide.installing_xpressive"></a><a class="link" href="user_s_guide.html#boost_xpressive.user_s_guide.installing_xpressive" title="Installing xpressive">Installing
xpressive</a>
</h3></div></div></div>
<h3>
<a name="boost_xpressive.user_s_guide.installing_xpressive.h0"></a>
<span class="phrase"><a name="boost_xpressive.user_s_guide.installing_xpressive.getting_xpressive"></a></span><a class="link" href="user_s_guide.html#boost_xpressive.user_s_guide.installing_xpressive.getting_xpressive">Getting
xpressive</a>
</h3>
<p>
There are two ways to get xpressive. The first and simplest is to download
the latest version of Boost. Just go to <a href="http://sf.net/projects/boost" target="_top">http://sf.net/projects/boost</a>
and follow the <span class="quote">“<span class="quote">Download</span>”</span> link.
</p>
<p>
The second way is by directly accessing the Boost Subversion repository.
Just go to <a href="http://svn.boost.org/trac/boost/" target="_top">http://svn.boost.org/trac/boost/</a>
and follow the instructions there for anonymous Subversion access. The version
in Boost Subversion is unstable.
</p>
<h3>
<a name="boost_xpressive.user_s_guide.installing_xpressive.h1"></a>
<span class="phrase"><a name="boost_xpressive.user_s_guide.installing_xpressive.building_with_xpressive"></a></span><a class="link" href="user_s_guide.html#boost_xpressive.user_s_guide.installing_xpressive.building_with_xpressive">Building
with xpressive</a>
</h3>
<p>
Xpressive is a header-only template library, which means you don't need to
alter your build scripts or link to any separate lib file to use it. All
you need to do is <code class="computeroutput"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">xpressive</span><span class="special">/</span><span class="identifier">xpressive</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span></code>.
If you are only using static regexes, you can improve compile times by only
including <code class="computeroutput"><span class="identifier">xpressive_static</span><span class="special">.</span><span class="identifier">hpp</span></code>. Likewise,
you can include <code class="computeroutput"><span class="identifier">xpressive_dynamic</span><span class="special">.</span><span class="identifier">hpp</span></code> if
you only plan on using dynamic regexes.
</p>
<p>
If you would also like to use semantic actions or custom assertions with
your static regexes, you will need to additionally include <code class="computeroutput"><span class="identifier">regex_actions</span><span class="special">.</span><span class="identifier">hpp</span></code>.
</p>
<h3>
<a name="boost_xpressive.user_s_guide.installing_xpressive.h2"></a>
<span class="phrase"><a name="boost_xpressive.user_s_guide.installing_xpressive.requirements"></a></span><a class="link" href="user_s_guide.html#boost_xpressive.user_s_guide.installing_xpressive.requirements">Requirements</a>
</h3>
<p>
Xpressive requires Boost version 1.34.1 or higher.
</p>
<h3>
<a name="boost_xpressive.user_s_guide.installing_xpressive.h3"></a>
<span class="phrase"><a name="boost_xpressive.user_s_guide.installing_xpressive.supported_compilers"></a></span><a class="link" href="user_s_guide.html#boost_xpressive.user_s_guide.installing_xpressive.supported_compilers">Supported
Compilers</a>
</h3>
<p>
Currently, Boost.Xpressive is known to work on the following compilers:
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
Visual C++ 7.1 and higher
</li>
<li class="listitem">
GNU C++ 3.4 and higher
</li>
<li class="listitem">
Intel for Linux 8.1 and higher
</li>
<li class="listitem">
Intel for Windows 10 and higher
</li>
<li class="listitem">
tru64cxx 71 and higher
</li>
<li class="listitem">
MinGW 3.4 and higher
</li>
<li class="listitem">
HP C/aC++ A.06.14
</li>
</ul></div>
<p>
Check the latest tests results at Boost's <a href="http://beta.boost.org/development/tests/trunk/developer/xpressive.html" target="_top">Regression
Results Page</a>.
</p>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
Please send any questions, comments and bug reports to eric <at>
boost-consulting <dot> com.
</p></td></tr>
</table></div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_xpressive.user_s_guide.quick_start"></a><a class="link" href="user_s_guide.html#boost_xpressive.user_s_guide.quick_start" title="Quick Start">Quick Start</a>
</h3></div></div></div>
<p>
You don't need to know much to start being productive with xpressive. Let's
begin with the nickel tour of the types and algorithms xpressive provides.
</p>
<div class="table">
<a name="boost_xpressive.user_s_guide.quick_start.t0"></a><p class="title"><b>Table 39.1. xpressive's Tool-Box</b></p>
<div class="table-contents"><table class="table" summary="xpressive's Tool-Box">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
<p>
Tool
</p>
</th>
<th>
<p>
Description
</p>
</th>
</tr></thead>
<tbody>
<tr>
<td>
<p>
<code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/basic_regex.html" title="Struct template basic_regex">basic_regex<></a></code></code>
</p>
</td>
<td>
<p>
Contains a compiled regular expression. <code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/basic_regex.html" title="Struct template basic_regex">basic_regex<></a></code></code>
is the most important type in xpressive. Everything you do with
xpressive will begin with creating an object of type <code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/basic_regex.html" title="Struct template basic_regex">basic_regex<></a></code></code>.
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/match_results.html" title="Struct template match_results">match_results<></a></code></code>,
<code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/sub_match.html" title="Struct template sub_match">sub_match<></a></code></code>
</p>
</td>
<td>
<p>
<code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/match_results.html" title="Struct template match_results">match_results<></a></code></code>
contains the results of a <code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/regex_match.html" title="Function regex_match">regex_match()</a></code></code>
or <code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/regex_search.html" title="Function regex_search">regex_search()</a></code></code>
operation. It acts like a vector of <code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/sub_match.html" title="Struct template sub_match">sub_match<></a></code></code>
objects. A <code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/sub_match.html" title="Struct template sub_match">sub_match<></a></code></code>
object contains a marked sub-expression (also known as a back-reference
in Perl). It is basically just a pair of iterators representing
the begin and end of the marked sub-expression.
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/regex_match.html" title="Function regex_match">regex_match()</a></code></code>
</p>
</td>
<td>
<p>
Checks to see if a string matches a regex. For <code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/regex_match.html" title="Function regex_match">regex_match()</a></code></code>
to succeed, the <span class="emphasis"><em>whole string</em></span> must match the
regex, from beginning to end. If you give <code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/regex_match.html" title="Function regex_match">regex_match()</a></code></code>
a <code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/match_results.html" title="Struct template match_results">match_results<></a></code></code>,
it will write into it any marked sub-expressions it finds.
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/regex_search.html" title="Function regex_search">regex_search()</a></code></code>
</p>
</td>
<td>
<p>
Searches a string to find a sub-string that matches the regex.
<code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/regex_search.html" title="Function regex_search">regex_search()</a></code></code>
will try to find a match at every position in the string, starting
at the beginning, and stopping when it finds a match or when the
string is exhausted. As with <code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/regex_match.html" title="Function regex_match">regex_match()</a></code></code>,
if you give <code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/regex_search.html" title="Function regex_search">regex_search()</a></code></code>
a <code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/match_results.html" title="Struct template match_results">match_results<></a></code></code>,
it will write into it any marked sub-expressions it finds.
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/regex_replace.html" title="Function regex_replace">regex_replace()</a></code></code>
</p>
</td>
<td>
<p>
Given an input string, a regex, and a substitution string, <code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/regex_replace.html" title="Function regex_replace">regex_replace()</a></code></code>
builds a new string by replacing those parts of the input string
that match the regex with the substitution string. The substitution
string can contain references to marked sub-expressions.
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/regex_iterator.html" title="Struct template regex_iterator">regex_iterator<></a></code></code>
</p>
</td>
<td>
<p>
An STL-compatible iterator that makes it easy to find all the places
in a string that match a regex. Dereferencing a <code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/regex_iterator.html" title="Struct template regex_iterator">regex_iterator<></a></code></code>
returns a <code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/match_results.html" title="Struct template match_results">match_results<></a></code></code>.
Incrementing a <code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/regex_iterator.html" title="Struct template regex_iterator">regex_iterator<></a></code></code>
finds the next match.
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/regex_token_iterator.html" title="Struct template regex_token_iterator">regex_token_iterator<></a></code></code>
</p>
</td>
<td>
<p>
Like <code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/regex_iterator.html" title="Struct template regex_iterator">regex_iterator<></a></code></code>,
except dereferencing a <code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/regex_token_iterator.html" title="Struct template regex_token_iterator">regex_token_iterator<></a></code></code>
returns a string. By default, it will return the whole sub-string
that the regex matched, but it can be configured to return any
or all of the marked sub-expressions one at a time, or even the
parts of the string that <span class="emphasis"><em>didn't</em></span> match the
regex.
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/regex_compiler.html" title="Struct template regex_compiler">regex_compiler<></a></code></code>
</p>
</td>
<td>
<p>
A factory for <code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/basic_regex.html" title="Struct template basic_regex">basic_regex<></a></code></code>
objects. It "compiles" a string into a regular expression.
You will not usually have to deal directly with <code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/regex_compiler.html" title="Struct template regex_compiler">regex_compiler<></a></code></code>
because the <code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/basic_regex.html" title="Struct template basic_regex">basic_regex<></a></code></code>
class has a factory method that uses <code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/regex_compiler.html" title="Struct template regex_compiler">regex_compiler<></a></code></code>
internally. But if you need to do anything fancy like create a
<code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/basic_regex.html" title="Struct template basic_regex">basic_regex<></a></code></code>
object with a different <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">locale</span></code>,
you will need to use a <code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/regex_compiler.html" title="Struct template regex_compiler">regex_compiler<></a></code></code>
explicitly.
</p>
</td>
</tr>
</tbody>
</table></div>
</div>
<br class="table-break"><p>
Now that you know a bit about the tools xpressive provides, you can pick
the right tool for you by answering the following two questions:
</p>
<div class="orderedlist"><ol class="orderedlist" type="1">
<li class="listitem">
What <span class="emphasis"><em>iterator</em></span> type will you use to traverse your
data?
</li>
<li class="listitem">
What do you want to <span class="emphasis"><em>do</em></span> to your data?
</li>
</ol></div>
<h3>
<a name="boost_xpressive.user_s_guide.quick_start.h0"></a>
<span class="phrase"><a name="boost_xpressive.user_s_guide.quick_start.know_your_iterator_type"></a></span><a class="link" href="user_s_guide.html#boost_xpressive.user_s_guide.quick_start.know_your_iterator_type">Know
Your Iterator Type</a>
</h3>
<p>
Most of the classes in xpressive are templates that are parameterized on
the iterator type. xpressive defines some common typedefs to make the job
of choosing the right types easier. You can use the table below to find the
right types based on the type of your iterator.
</p>
<div class="table">
<a name="boost_xpressive.user_s_guide.quick_start.t1"></a><p class="title"><b>Table 39.2. xpressive Typedefs vs. Iterator Types</b></p>
<div class="table-contents"><table class="table" summary="xpressive Typedefs vs. Iterator Types">
<colgroup>
<col>
<col>
<col>
<col>
<col>
</colgroup>
<thead><tr>
<th>
</th>
<th>
<p>
std::string::const_iterator
</p>
</th>
<th>
<p>
char const *
</p>
</th>
<th>
<p>
std::wstring::const_iterator
</p>
</th>
<th>
<p>
wchar_t const *
</p>
</th>
</tr></thead>
<tbody>
<tr>
<td>
<p>
<code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/basic_regex.html" title="Struct template basic_regex">basic_regex<></a></code></code>
</p>
</td>
<td>
<p>
<code class="computeroutput"><span class="identifier">sregex</span></code>
</p>
</td>
<td>
<p>
<code class="computeroutput"><span class="identifier">cregex</span></code>
</p>
</td>
<td>
<p>
<code class="computeroutput"><span class="identifier">wsregex</span></code>
</p>
</td>
<td>
<p>
<code class="computeroutput"><span class="identifier">wcregex</span></code>
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/match_results.html" title="Struct template match_results">match_results<></a></code></code>
</p>
</td>
<td>
<p>
<code class="computeroutput"><span class="identifier">smatch</span></code>
</p>
</td>
<td>
<p>
<code class="computeroutput"><span class="identifier">cmatch</span></code>
</p>
</td>
<td>
<p>
<code class="computeroutput"><span class="identifier">wsmatch</span></code>
</p>
</td>
<td>
<p>
<code class="computeroutput"><span class="identifier">wcmatch</span></code>
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/regex_compiler.html" title="Struct template regex_compiler">regex_compiler<></a></code></code>
</p>
</td>
<td>
<p>
<code class="computeroutput"><span class="identifier">sregex_compiler</span></code>
</p>
</td>
<td>
<p>
<code class="computeroutput"><span class="identifier">cregex_compiler</span></code>
</p>
</td>
<td>
<p>
<code class="computeroutput"><span class="identifier">wsregex_compiler</span></code>
</p>
</td>
<td>
<p>
<code class="computeroutput"><span class="identifier">wcregex_compiler</span></code>
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/regex_iterator.html" title="Struct template regex_iterator">regex_iterator<></a></code></code>
</p>
</td>
<td>
<p>
<code class="computeroutput"><span class="identifier">sregex_iterator</span></code>
</p>
</td>
<td>
<p>
<code class="computeroutput"><span class="identifier">cregex_iterator</span></code>
</p>
</td>
<td>
<p>
<code class="computeroutput"><span class="identifier">wsregex_iterator</span></code>
</p>
</td>
<td>
<p>
<code class="computeroutput"><span class="identifier">wcregex_iterator</span></code>
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/regex_token_iterator.html" title="Struct template regex_token_iterator">regex_token_iterator<></a></code></code>
</p>
</td>
<td>
<p>
<code class="computeroutput"><span class="identifier">sregex_token_iterator</span></code>
</p>
</td>
<td>
<p>
<code class="computeroutput"><span class="identifier">cregex_token_iterator</span></code>
</p>
</td>
<td>
<p>
<code class="computeroutput"><span class="identifier">wsregex_token_iterator</span></code>
</p>
</td>
<td>
<p>
<code class="computeroutput"><span class="identifier">wcregex_token_iterator</span></code>
</p>
</td>
</tr>
</tbody>
</table></div>
</div>
<br class="table-break"><p>
You should notice the systematic naming convention. Many of these types are
used together, so the naming convention helps you to use them consistently.
For instance, if you have a <code class="computeroutput"><span class="identifier">sregex</span></code>,
you should also be using a <code class="computeroutput"><span class="identifier">smatch</span></code>.
</p>
<p>
If you are not using one of those four iterator types, then you can use the
templates directly and specify your iterator type.
</p>
<h3>
<a name="boost_xpressive.user_s_guide.quick_start.h1"></a>
<span class="phrase"><a name="boost_xpressive.user_s_guide.quick_start.know_your_task"></a></span><a class="link" href="user_s_guide.html#boost_xpressive.user_s_guide.quick_start.know_your_task">Know Your
Task</a>
</h3>
<p>
Do you want to find a pattern once? Many times? Search and replace? xpressive
has tools for all that and more. Below is a quick reference:
</p>
<div class="table">
<a name="boost_xpressive.user_s_guide.quick_start.t2"></a><p class="title"><b>Table 39.3. Tasks and Tools</b></p>
<div class="table-contents"><table class="table" summary="Tasks and Tools">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
<p>
To do this ...
</p>
</th>
<th>
<p>
Use this ...
</p>
</th>
</tr></thead>
<tbody>
<tr>
<td>
<p>
<span class="inlinemediaobject"><img src="../images/tip.png" alt="tip"></span> <a class="link" href="user_s_guide.html#boost_xpressive.user_s_guide.examples.see_if_a_whole_string_matches_a_regex">See
if a whole string matches a regex</a>
</p>
</td>
<td>
<p>
The <code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/regex_match.html" title="Function regex_match">regex_match()</a></code></code>
algorithm
</p>
</td>
</tr>
<tr>
<td>
<p>
<span class="inlinemediaobject"><img src="../images/tip.png" alt="tip"></span> <a class="link" href="user_s_guide.html#boost_xpressive.user_s_guide.examples.see_if_a_string_contains_a_sub_string_that_matches_a_regex">See
if a string contains a sub-string that matches a regex</a>
</p>
</td>
<td>
<p>
The <code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/regex_search.html" title="Function regex_search">regex_search()</a></code></code>
algorithm
</p>
</td>
</tr>
<tr>
<td>
<p>
<span class="inlinemediaobject"><img src="../images/tip.png" alt="tip"></span> <a class="link" href="user_s_guide.html#boost_xpressive.user_s_guide.examples.replace_all_sub_strings_that_match_a_regex">Replace
all sub-strings that match a regex</a>
</p>
</td>
<td>
<p>
The <code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/regex_replace.html" title="Function regex_replace">regex_replace()</a></code></code>
algorithm
</p>
</td>
</tr>
<tr>
<td>
<p>
<span class="inlinemediaobject"><img src="../images/tip.png" alt="tip"></span> <a class="link" href="user_s_guide.html#boost_xpressive.user_s_guide.examples.find_all_the_sub_strings_that_match_a_regex_and_step_through_them_one_at_a_time">Find
all the sub-strings that match a regex and step through them one
at a time</a>
</p>
</td>
<td>
<p>
The <code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/regex_iterator.html" title="Struct template regex_iterator">regex_iterator<></a></code></code>
class
</p>
</td>
</tr>
<tr>
<td>
<p>
<span class="inlinemediaobject"><img src="../images/tip.png" alt="tip"></span> <a class="link" href="user_s_guide.html#boost_xpressive.user_s_guide.examples.split_a_string_into_tokens_that_each_match_a_regex">Split
a string into tokens that each match a regex</a>
</p>
</td>
<td>
<p>
The <code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/regex_token_iterator.html" title="Struct template regex_token_iterator">regex_token_iterator<></a></code></code>
class
</p>
</td>
</tr>
<tr>
<td>
<p>
<span class="inlinemediaobject"><img src="../images/tip.png" alt="tip"></span> <a class="link" href="user_s_guide.html#boost_xpressive.user_s_guide.examples.split_a_string_using_a_regex_as_a_delimiter">Split
a string using a regex as a delimiter</a>
</p>
</td>
<td>
<p>
The <code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/regex_token_iterator.html" title="Struct template regex_token_iterator">regex_token_iterator<></a></code></code>
class
</p>
</td>
</tr>
</tbody>
</table></div>
</div>
<br class="table-break"><p>
These algorithms and classes are described in excruciating detail in the
Reference section.
</p>
<div class="tip"><table border="0" summary="Tip">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Tip]" src="../../../doc/src/images/tip.png"></td>
<th align="left">Tip</th>
</tr>
<tr><td align="left" valign="top"><p>
Try clicking on a task in the table above to see a complete example program
that uses xpressive to solve that particular task.
</p></td></tr>
</table></div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="xpressive.user_s_guide.creating_a_regex_object"></a><a class="link" href="user_s_guide.html#xpressive.user_s_guide.creating_a_regex_object" title="Creating a Regex Object">Creating
a Regex Object</a>
</h3></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="user_s_guide.html#boost_xpressive.user_s_guide.creating_a_regex_object.static_regexes">Static
Regexes</a></span></dt>
<dt><span class="section"><a href="user_s_guide.html#boost_xpressive.user_s_guide.creating_a_regex_object.dynamic_regexes">Dynamic
Regexes</a></span></dt>
</dl></div>
<p>
When using xpressive, the first thing you'll do is create a <code class="literal"><code class="computeroutput"><a class="link" href="../boost/xpressive/basic_regex.html" title="Struct template basic_regex">basic_regex<></a></code></code>
object. This section goes over the nuts and bolts of building a regular expression
in the two dialects xpressive supports: static and dynamic.
</p>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="boost_xpressive.user_s_guide.creating_a_regex_object.static_regexes"></a><a class="link" href="user_s_guide.html#boost_xpressive.user_s_guide.creating_a_regex_object.static_regexes" title="Static Regexes">Static
Regexes</a>
</h4></div></div></div>
<h3>
<a name="boost_xpressive.user_s_guide.creating_a_regex_object.static_regexes.h0"></a>
<span class="phrase"><a name="boost_xpressive.user_s_guide.creating_a_regex_object.static_regexes.overview"></a></span><a class="link" href="user_s_guide.html#boost_xpressive.user_s_guide.creating_a_regex_object.static_regexes.overview">Overview</a>
</h3>
<p>
The feature that really sets xpressive apart from other C/C++ regular expression
libraries is the ability to author a regular expression using C++ expressions.
xpressive achieves this through operator overloading, using a technique
called <span class="emphasis"><em>expression templates</em></span> to embed a mini-language
dedicated to pattern matching within C++. These "static regexes"
have many advantages over their string-based brethren. In particular, static
regexes:
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">