• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>regex_replace</title>
5<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7<link rel="home" href="../../index.html" title="Boost.Regex 5.1.4">
8<link rel="up" href="../ref.html" title="Reference">
9<link rel="prev" href="regex_search.html" title="regex_search">
10<link rel="next" href="regex_iterator.html" title="regex_iterator">
11</head>
12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13<table cellpadding="2" width="100%"><tr>
14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
15<td align="center"><a href="../../../../../../index.html">Home</a></td>
16<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
19<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
20</tr></table>
21<hr>
22<div class="spirit-nav">
23<a accesskey="p" href="regex_search.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../ref.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="regex_iterator.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h3 class="title">
27<a name="boost_regex.ref.regex_replace"></a><a class="link" href="regex_replace.html" title="regex_replace">regex_replace</a>
28</h3></div></div></div>
29<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">regex</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
30</pre>
31<p>
32        The algorithm <a class="link" href="regex_replace.html" title="regex_replace"><code class="computeroutput"><span class="identifier">regex_replace</span></code></a> searches through a
33        string finding all the matches to the regular expression: for each match
34        it then calls <a class="link" href="match_results.html#boost_regex.match_results_format"><code class="computeroutput"><span class="identifier">match_results</span><span class="special">&lt;&gt;::</span><span class="identifier">format</span></code></a> to format the string and
35        sends the result to the output iterator. Sections of text that do not match
36        are copied to the output unchanged only if the <span class="emphasis"><em>flags</em></span>
37        parameter does not have the flag <code class="computeroutput"><span class="identifier">format_no_copy</span></code>
38        set. If the flag <code class="computeroutput"><span class="identifier">format_first_only</span></code>
39        is set then only the first occurrence is replaced rather than all occurrences.
40      </p>
41<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">OutputIterator</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">BidirectionalIterator</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">traits</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Formatter</span><span class="special">&gt;</span>
42<span class="identifier">OutputIterator</span> <span class="identifier">regex_replace</span><span class="special">(</span><span class="identifier">OutputIterator</span> <span class="identifier">out</span><span class="special">,</span>
43                             <span class="identifier">BidirectionalIterator</span> <span class="identifier">first</span><span class="special">,</span>
44                             <span class="identifier">BidirectionalIterator</span> <span class="identifier">last</span><span class="special">,</span>
45                             <span class="keyword">const</span> <span class="identifier">basic_regex</span><span class="special">&lt;</span><span class="identifier">charT</span><span class="special">,</span> <span class="identifier">traits</span><span class="special">&gt;&amp;</span> <span class="identifier">e</span><span class="special">,</span>
46                             <span class="identifier">Formatter</span> <span class="identifier">fmt</span><span class="special">,</span>
47                             <span class="identifier">match_flag_type</span> <span class="identifier">flags</span> <span class="special">=</span> <span class="identifier">match_default</span><span class="special">);</span>
48
49<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">traits</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Formatter</span><span class="special">&gt;</span>
50<span class="identifier">basic_string</span><span class="special">&lt;</span><span class="identifier">charT</span><span class="special">&gt;</span> <span class="identifier">regex_replace</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">basic_string</span><span class="special">&lt;</span><span class="identifier">charT</span><span class="special">&gt;&amp;</span> <span class="identifier">s</span><span class="special">,</span>
51                                  <span class="keyword">const</span> <span class="identifier">basic_regex</span><span class="special">&lt;</span><span class="identifier">charT</span><span class="special">,</span> <span class="identifier">traits</span><span class="special">&gt;&amp;</span> <span class="identifier">e</span><span class="special">,</span>
52                                  <span class="identifier">Formatter</span> <span class="identifier">fmt</span><span class="special">,</span>
53                                  <span class="identifier">match_flag_type</span> <span class="identifier">flags</span> <span class="special">=</span> <span class="identifier">match_default</span><span class="special">);</span>
54</pre>
55<h5>
56<a name="boost_regex.ref.regex_replace.h0"></a>
57        <span class="phrase"><a name="boost_regex.ref.regex_replace.description"></a></span><a class="link" href="regex_replace.html#boost_regex.ref.regex_replace.description">Description</a>
58      </h5>
59<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">OutputIterator</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">BidirectionalIterator</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">traits</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Formatter</span><span class="special">&gt;</span>
60<span class="identifier">OutputIterator</span> <span class="identifier">regex_replace</span><span class="special">(</span><span class="identifier">OutputIterator</span> <span class="identifier">out</span><span class="special">,</span>
61                             <span class="identifier">BidirectionalIterator</span> <span class="identifier">first</span><span class="special">,</span>
62                             <span class="identifier">BidirectionalIterator</span> <span class="identifier">last</span><span class="special">,</span>
63                             <span class="keyword">const</span> <span class="identifier">basic_regex</span><span class="special">&lt;</span><span class="identifier">charT</span><span class="special">,</span> <span class="identifier">traits</span><span class="special">&gt;&amp;</span> <span class="identifier">e</span><span class="special">,</span>
64                             <span class="identifier">Formatter</span> <span class="identifier">fmt</span><span class="special">,</span>
65                             <span class="identifier">match_flag_type</span> <span class="identifier">flags</span> <span class="special">=</span> <span class="identifier">match_default</span><span class="special">);</span>
66</pre>
67<p>
68        Enumerates all the occurrences of expression <span class="emphasis"><em>e</em></span> in the
69        sequence [first, last), replacing each occurrence with the string that results
70        by merging the match found with the format string <span class="emphasis"><em>fmt</em></span>,
71        and copies the resulting string to <span class="emphasis"><em>out</em></span>. In the case
72        that <span class="emphasis"><em>fmt</em></span> is a unary, binary or ternary function object,
73        then the character sequence generated by that object is copied unchanged
74        to the output when performing a substitution.
75      </p>
76<p>
77        If the flag <code class="computeroutput"><span class="identifier">format_no_copy</span></code>
78        is set in <span class="emphasis"><em>flags</em></span> then unmatched sections of text are
79        not copied to output.
80      </p>
81<p>
82        If the flag <code class="computeroutput"><span class="identifier">format_first_only</span></code>
83        is set in flags then only the first occurrence of <span class="emphasis"><em>e</em></span>
84        is replaced.
85      </p>
86<p>
87        The manner in which the format string <span class="emphasis"><em>fmt</em></span> is interpreted,
88        along with the rules used for finding matches, are determined by the flags
89        set in <span class="emphasis"><em>flags</em></span>: see <a class="link" href="match_flag_type.html" title="match_flag_type"><code class="computeroutput"><span class="identifier">match_flag_type</span></code></a>.
90      </p>
91<p>
92        <span class="bold"><strong>Requires</strong></span> The type <code class="computeroutput"><span class="identifier">Formatter</span></code>
93        must be either a pointer to a null-terminated string of type <code class="computeroutput"><span class="identifier">char_type</span><span class="special">[]</span></code>,
94        or be a container of <code class="computeroutput"><span class="identifier">char_type</span></code>'s
95        (for example <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">basic_string</span><span class="special">&lt;</span><span class="identifier">char_type</span><span class="special">&gt;</span></code>)
96        or be a unary, binary or ternary functor that computes the replacement string
97        from a function call: either <code class="computeroutput"><span class="identifier">fmt</span><span class="special">(</span><span class="identifier">what</span><span class="special">)</span></code>
98        which must return a container of <code class="computeroutput"><span class="identifier">char_type</span></code>'s
99        to be used as the replacement text, or either <code class="computeroutput"><span class="identifier">fmt</span><span class="special">(</span><span class="identifier">what</span><span class="special">,</span>
100        <span class="identifier">out</span><span class="special">)</span></code>
101        or <code class="computeroutput"><span class="identifier">fmt</span><span class="special">(</span><span class="identifier">what</span><span class="special">,</span> <span class="identifier">out</span><span class="special">,</span> <span class="identifier">flags</span><span class="special">)</span></code>, both of which write the replacement text
102        to <code class="computeroutput"><span class="special">*</span><span class="identifier">out</span></code>,
103        and then return the new OutputIterator position. In each case <code class="computeroutput"><span class="identifier">what</span></code> is the <a class="link" href="match_results.html" title="match_results"><code class="computeroutput"><span class="identifier">match_results</span></code></a> object that represents
104        the match found. Note that if the formatter is a functor, then it is <span class="emphasis"><em>passed
105        by value</em></span>: users that want to pass function objects with internal
106        state might want to use <a href="../../../../../../doc/html/ref.html" target="_top">Boost.Ref</a>
107        to wrap the object so that it's passed by reference.
108      </p>
109<p>
110        <span class="bold"><strong>Effects</strong></span>: Constructs an <a class="link" href="regex_iterator.html" title="regex_iterator"><code class="computeroutput"><span class="identifier">regex_iterator</span></code></a> object:
111      </p>
112<pre class="programlisting"><span class="identifier">regex_iterator</span><span class="special">&lt;</span><span class="identifier">BidirectionalIterator</span><span class="special">,</span> <span class="identifier">charT</span><span class="special">,</span> <span class="identifier">traits</span><span class="special">,</span> <span class="identifier">Allocator</span><span class="special">&gt;</span>
113                                          <span class="identifier">i</span><span class="special">(</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">last</span><span class="special">,</span> <span class="identifier">e</span><span class="special">,</span> <span class="identifier">flags</span><span class="special">),</span>
114</pre>
115<p>
116        and uses <span class="emphasis"><em>i</em></span> to enumerate through all of the matches
117        <span class="emphasis"><em>m</em></span> of type <a class="link" href="match_results.html" title="match_results"><code class="computeroutput"><span class="identifier">match_results</span></code></a> <code class="computeroutput"><span class="special">&lt;</span><span class="identifier">BidirectionalIterator</span><span class="special">&gt;</span></code>
118        that occur within the sequence [first, last).
119      </p>
120<p>
121        If no such matches are found and
122      </p>
123<pre class="programlisting"><span class="special">!(</span><span class="identifier">flags</span> <span class="special">&amp;</span> <span class="identifier">format_no_copy</span><span class="special">)</span>
124</pre>
125<p>
126        then calls
127      </p>
128<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">copy</span><span class="special">(</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">last</span><span class="special">,</span> <span class="identifier">out</span><span class="special">).</span>
129</pre>
130<p>
131        Otherwise, for each match found, if
132      </p>
133<pre class="programlisting"><span class="special">!(</span><span class="identifier">flags</span> <span class="special">&amp;</span> <span class="identifier">format_no_copy</span><span class="special">)</span>
134</pre>
135<p>
136        calls
137      </p>
138<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">copy</span><span class="special">(</span><span class="identifier">m</span><span class="special">.</span><span class="identifier">prefix</span><span class="special">().</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">m</span><span class="special">.</span><span class="identifier">prefix</span><span class="special">().</span><span class="identifier">last</span><span class="special">,</span> <span class="identifier">out</span><span class="special">),</span>
139</pre>
140<p>
141        and then calls
142      </p>
143<pre class="programlisting"><span class="identifier">m</span><span class="special">.</span><span class="identifier">format</span><span class="special">(</span><span class="identifier">out</span><span class="special">,</span> <span class="identifier">fmt</span><span class="special">,</span> <span class="identifier">flags</span><span class="special">).</span>
144</pre>
145<p>
146        Finally if
147      </p>
148<pre class="programlisting"><span class="special">!(</span><span class="identifier">flags</span> <span class="special">&amp;</span> <span class="identifier">format_no_copy</span><span class="special">)</span>
149</pre>
150<p>
151        calls
152      </p>
153<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">copy</span><span class="special">(</span><span class="identifier">last_m</span><span class="special">.</span><span class="identifier">suffix</span><span class="special">().</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">last_m</span><span class="special">,</span><span class="identifier">suffix</span><span class="special">().</span><span class="identifier">last</span><span class="special">,</span> <span class="identifier">out</span><span class="special">)</span>
154</pre>
155<p>
156        where <span class="emphasis"><em>last_m</em></span> is a copy of the last match found.
157      </p>
158<p>
159        If <code class="computeroutput"><span class="identifier">flags</span> <span class="special">&amp;</span>
160        <span class="identifier">format_first_only</span></code> is non-zero then
161        only the first match found is replaced.
162      </p>
163<p>
164        <span class="bold"><strong>Throws</strong></span>: <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">runtime_error</span></code>
165        if the complexity of matching the expression against an N character string
166        begins to exceed O(N<sup>2</sup>), or if the program runs out of stack space while matching
167        the expression (if Boost.Regex is configured in recursive mode), or if the
168        matcher exhausts its permitted memory allocation (if Boost.Regex is configured
169        in non-recursive mode).
170      </p>
171<p>
172        <span class="bold"><strong>Returns</strong></span>: out.
173      </p>
174<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">traits</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Formatter</span><span class="special">&gt;</span>
175<span class="identifier">basic_string</span><span class="special">&lt;</span><span class="identifier">charT</span><span class="special">&gt;</span> <span class="identifier">regex_replace</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">basic_string</span><span class="special">&lt;</span><span class="identifier">charT</span><span class="special">&gt;&amp;</span> <span class="identifier">s</span><span class="special">,</span>
176                                  <span class="keyword">const</span> <span class="identifier">basic_regex</span><span class="special">&lt;</span><span class="identifier">charT</span><span class="special">,</span> <span class="identifier">traits</span><span class="special">&gt;&amp;</span> <span class="identifier">e</span><span class="special">,</span>
177                                  <span class="identifier">Formatter</span> <span class="identifier">fmt</span><span class="special">,</span>
178                                  <span class="identifier">match_flag_type</span> <span class="identifier">flags</span> <span class="special">=</span> <span class="identifier">match_default</span><span class="special">);</span>
179</pre>
180<p>
181        <span class="bold"><strong>Requires</strong></span> The type <code class="computeroutput"><span class="identifier">Formatter</span></code>
182        must be either a pointer to a null-terminated string of type <code class="computeroutput"><span class="identifier">char_type</span><span class="special">[]</span></code>,
183        or be a container of <code class="computeroutput"><span class="identifier">char_type</span></code>'s
184        (for example <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">basic_string</span><span class="special">&lt;</span><span class="identifier">char_type</span><span class="special">&gt;</span></code>)
185        or be a unary, binary or ternary functor that computes the replacement string
186        from a function call: either <code class="computeroutput"><span class="identifier">fmt</span><span class="special">(</span><span class="identifier">what</span><span class="special">)</span></code>
187        which must return a container of <code class="computeroutput"><span class="identifier">char_type</span></code>'s
188        to be used as the replacement text, or either <code class="computeroutput"><span class="identifier">fmt</span><span class="special">(</span><span class="identifier">what</span><span class="special">,</span>
189        <span class="identifier">out</span><span class="special">)</span></code>
190        or <code class="computeroutput"><span class="identifier">fmt</span><span class="special">(</span><span class="identifier">what</span><span class="special">,</span> <span class="identifier">out</span><span class="special">,</span> <span class="identifier">flags</span><span class="special">)</span></code>, both of which write the replacement text
191        to <code class="computeroutput"><span class="special">*</span><span class="identifier">out</span></code>,
192        and then return the new OutputIterator position. In each case <code class="computeroutput"><span class="identifier">what</span></code> is the <a class="link" href="match_results.html" title="match_results"><code class="computeroutput"><span class="identifier">match_results</span></code></a> object that represents
193        the match found.
194      </p>
195<p>
196        <span class="bold"><strong>Effects</strong></span>: Constructs an object <code class="computeroutput"><span class="identifier">basic_string</span><span class="special">&lt;</span><span class="identifier">charT</span><span class="special">&gt;</span> <span class="identifier">result</span></code>, calls <code class="computeroutput"><span class="identifier">regex_replace</span><span class="special">(</span><span class="identifier">back_inserter</span><span class="special">(</span><span class="identifier">result</span><span class="special">),</span> <span class="identifier">s</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">s</span><span class="special">.</span><span class="identifier">end</span><span class="special">(),</span> <span class="identifier">e</span><span class="special">,</span>
197        <span class="identifier">fmt</span><span class="special">,</span>
198        <span class="identifier">flags</span><span class="special">)</span></code>,
199        and then returns <code class="computeroutput"><span class="identifier">result</span></code>.
200      </p>
201<h5>
202<a name="boost_regex.ref.regex_replace.h1"></a>
203        <span class="phrase"><a name="boost_regex.ref.regex_replace.examples"></a></span><a class="link" href="regex_replace.html#boost_regex.ref.regex_replace.examples">Examples</a>
204      </h5>
205<p>
206        The following example takes C/C++ source code as input, and outputs syntax
207        highlighted HTML code.
208      </p>
209<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">fstream</span><span class="special">&gt;</span>
210<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">sstream</span><span class="special">&gt;</span>
211<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">string</span><span class="special">&gt;</span>
212<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iterator</span><span class="special">&gt;</span>
213<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">regex</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
214<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">fstream</span><span class="special">&gt;</span>
215<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>
216
217<span class="comment">// purpose:</span>
218<span class="comment">// takes the contents of a file and transform to</span>
219<span class="comment">// syntax highlighted code in html format</span>
220
221<span class="identifier">boost</span><span class="special">::</span><span class="identifier">regex</span> <span class="identifier">e1</span><span class="special">,</span> <span class="identifier">e2</span><span class="special">;</span>
222<span class="keyword">extern</span> <span class="keyword">const</span> <span class="keyword">char</span><span class="special">*</span> <span class="identifier">expression_text</span><span class="special">;</span>
223<span class="keyword">extern</span> <span class="keyword">const</span> <span class="keyword">char</span><span class="special">*</span> <span class="identifier">format_string</span><span class="special">;</span>
224<span class="keyword">extern</span> <span class="keyword">const</span> <span class="keyword">char</span><span class="special">*</span> <span class="identifier">pre_expression</span><span class="special">;</span>
225<span class="keyword">extern</span> <span class="keyword">const</span> <span class="keyword">char</span><span class="special">*</span> <span class="identifier">pre_format</span><span class="special">;</span>
226<span class="keyword">extern</span> <span class="keyword">const</span> <span class="keyword">char</span><span class="special">*</span> <span class="identifier">header_text</span><span class="special">;</span>
227<span class="keyword">extern</span> <span class="keyword">const</span> <span class="keyword">char</span><span class="special">*</span> <span class="identifier">footer_text</span><span class="special">;</span>
228
229<span class="keyword">void</span> <span class="identifier">load_file</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">&amp;</span> <span class="identifier">s</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">istream</span><span class="special">&amp;</span> <span class="identifier">is</span><span class="special">)</span>
230<span class="special">{</span>
231   <span class="identifier">s</span><span class="special">.</span><span class="identifier">erase</span><span class="special">();</span>
232   <span class="identifier">s</span><span class="special">.</span><span class="identifier">reserve</span><span class="special">(</span><span class="identifier">is</span><span class="special">.</span><span class="identifier">rdbuf</span><span class="special">()-&gt;</span><span class="identifier">in_avail</span><span class="special">());</span>
233   <span class="keyword">char</span> <span class="identifier">c</span><span class="special">;</span>
234   <span class="keyword">while</span><span class="special">(</span><span class="identifier">is</span><span class="special">.</span><span class="identifier">get</span><span class="special">(</span><span class="identifier">c</span><span class="special">))</span>
235   <span class="special">{</span>
236      <span class="keyword">if</span><span class="special">(</span><span class="identifier">s</span><span class="special">.</span><span class="identifier">capacity</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">s</span><span class="special">.</span><span class="identifier">size</span><span class="special">())</span>
237         <span class="identifier">s</span><span class="special">.</span><span class="identifier">reserve</span><span class="special">(</span><span class="identifier">s</span><span class="special">.</span><span class="identifier">capacity</span><span class="special">()</span> <span class="special">*</span> <span class="number">3</span><span class="special">);</span>
238      <span class="identifier">s</span><span class="special">.</span><span class="identifier">append</span><span class="special">(</span><span class="number">1</span><span class="special">,</span> <span class="identifier">c</span><span class="special">);</span>
239   <span class="special">}</span>
240<span class="special">}</span>
241
242<span class="keyword">int</span> <span class="identifier">main</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">argc</span><span class="special">,</span> <span class="keyword">const</span> <span class="keyword">char</span><span class="special">**</span> <span class="identifier">argv</span><span class="special">)</span>
243<span class="special">{</span>
244   <span class="keyword">try</span><span class="special">{</span>
245   <span class="identifier">e1</span><span class="special">.</span><span class="identifier">assign</span><span class="special">(</span><span class="identifier">expression_text</span><span class="special">);</span>
246   <span class="identifier">e2</span><span class="special">.</span><span class="identifier">assign</span><span class="special">(</span><span class="identifier">pre_expression</span><span class="special">);</span>
247   <span class="keyword">for</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">1</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="identifier">argc</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span>
248   <span class="special">{</span>
249      <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Processing file "</span> <span class="special">&lt;&lt;</span> <span class="identifier">argv</span><span class="special">[</span><span class="identifier">i</span><span class="special">]</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
250      <span class="identifier">std</span><span class="special">::</span><span class="identifier">ifstream</span> <span class="identifier">fs</span><span class="special">(</span><span class="identifier">argv</span><span class="special">[</span><span class="identifier">i</span><span class="special">]);</span>
251      <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">in</span><span class="special">;</span>
252      <span class="identifier">load_file</span><span class="special">(</span><span class="identifier">in</span><span class="special">,</span> <span class="identifier">fs</span><span class="special">);</span>
253      <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">out_name</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">(</span><span class="identifier">argv</span><span class="special">[</span><span class="identifier">i</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="special">(</span><span class="string">".htm"</span><span class="special">));</span>
254      <span class="identifier">std</span><span class="special">::</span><span class="identifier">ofstream</span> <span class="identifier">os</span><span class="special">(</span><span class="identifier">out_name</span><span class="special">.</span><span class="identifier">c_str</span><span class="special">());</span>
255      <span class="identifier">os</span> <span class="special">&lt;&lt;</span> <span class="identifier">header_text</span><span class="special">;</span>
256      <span class="comment">// strip '&lt;' and '&gt;' first by outputting to a</span>
257      <span class="comment">// temporary string stream</span>
258      <span class="identifier">std</span><span class="special">::</span><span class="identifier">ostringstream</span> <span class="identifier">t</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">ios</span><span class="special">::</span><span class="identifier">out</span> <span class="special">|</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">ios</span><span class="special">::</span><span class="identifier">binary</span><span class="special">);</span>
259      <span class="identifier">std</span><span class="special">::</span><span class="identifier">ostream_iterator</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">,</span> <span class="keyword">char</span><span class="special">&gt;</span> <span class="identifier">oi</span><span class="special">(</span><span class="identifier">t</span><span class="special">);</span>
260      <span class="identifier">boost</span><span class="special">::</span><span class="identifier">regex_replace</span><span class="special">(</span><span class="identifier">oi</span><span class="special">,</span> <span class="identifier">in</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">in</span><span class="special">.</span><span class="identifier">end</span><span class="special">(),</span>
261      <span class="identifier">e2</span><span class="special">,</span> <span class="identifier">pre_format</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">match_default</span> <span class="special">|</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">format_all</span><span class="special">);</span>
262      <span class="comment">// then output to final output stream</span>
263      <span class="comment">// adding syntax highlighting:</span>
264      <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">s</span><span class="special">(</span><span class="identifier">t</span><span class="special">.</span><span class="identifier">str</span><span class="special">());</span>
265      <span class="identifier">std</span><span class="special">::</span><span class="identifier">ostream_iterator</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">,</span> <span class="keyword">char</span><span class="special">&gt;</span> <span class="identifier">out</span><span class="special">(</span><span class="identifier">os</span><span class="special">);</span>
266      <span class="identifier">boost</span><span class="special">::</span><span class="identifier">regex_replace</span><span class="special">(</span><span class="identifier">out</span><span class="special">,</span> <span class="identifier">s</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">s</span><span class="special">.</span><span class="identifier">end</span><span class="special">(),</span>
267      <span class="identifier">e1</span><span class="special">,</span> <span class="identifier">format_string</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">match_default</span> <span class="special">|</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">format_all</span><span class="special">);</span>
268      <span class="identifier">os</span> <span class="special">&lt;&lt;</span> <span class="identifier">footer_text</span><span class="special">;</span>
269   <span class="special">}</span>
270   <span class="special">}</span>
271   <span class="keyword">catch</span><span class="special">(...)</span>
272   <span class="special">{</span> <span class="keyword">return</span> <span class="special">-</span><span class="number">1</span><span class="special">;</span> <span class="special">}</span>
273   <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
274<span class="special">}</span>
275
276<span class="keyword">extern</span> <span class="keyword">const</span> <span class="keyword">char</span><span class="special">*</span> <span class="identifier">pre_expression</span> <span class="special">=</span> <span class="string">"(&lt;)|(&gt;)|(&amp;)|\\r"</span><span class="special">;</span>
277<span class="keyword">extern</span> <span class="keyword">const</span> <span class="keyword">char</span><span class="special">*</span> <span class="identifier">pre_format</span> <span class="special">=</span> <span class="string">"(?1&amp;lt;)(?2&amp;gt;)(?3&amp;amp;)"</span><span class="special">;</span>
278
279
280<span class="keyword">const</span> <span class="keyword">char</span><span class="special">*</span> <span class="identifier">expression_text</span> <span class="special">=</span>
281   <span class="comment">// preprocessor directives: index 1</span>
282   <span class="string">"(^[[:blank:]]*#(?:[^\\\\\\n]|\\\\[^\\n[:punct:][:word:]]*[\\n[:punct:][:word:]])*)|"</span>
283   <span class="comment">// comment: index 2</span>
284   <span class="string">"(//[^\\n]*|/\\*.*?\\*/)|"</span>
285   <span class="comment">// literals: index 3</span>
286   <span class="string">"\\&lt;([+-]?(?:(?:0x[[:xdigit:]]+)|(?:(?:[[:digit:]]*\\.)?[[:digit:]]+"</span>
287   <span class="string">"(?:[eE][+-]?[[:digit:]]+)?))u?(?:(?:int(?:8|16|32|64))|L)?)\\&gt;|"</span>
288   <span class="comment">// string literals: index 4</span>
289   <span class="string">"('(?:[^\\\\']|\\\\.)*'|\"(?:[^\\\\\"]|\\\\.)*\")|"</span>
290   <span class="comment">// keywords: index 5</span>
291   <span class="string">"\\&lt;(__asm|__cdecl|__declspec|__export|__far16|__fastcall|__fortran|__import"</span>
292   <span class="string">"|__pascal|__rtti|__stdcall|_asm|_cdecl|__except|_export|_far16|_fastcall"</span>
293   <span class="string">"|__finally|_fortran|_import|_pascal|_stdcall|__thread|__try|asm|auto|bool"</span>
294   <span class="string">"|break|case|catch|cdecl|char|class|const|const_cast|continue|default|delete"</span>
295   <span class="string">"|do|double|dynamic_cast|else|enum|explicit|extern|false|float|for|friend|goto"</span>
296   <span class="string">"|if|inline|int|long|mutable|namespace|new|operator|pascal|private|protected"</span>
297   <span class="string">"|public|register|reinterpret_cast|return|short|signed|sizeof|static|static_cast"</span>
298   <span class="string">"|struct|switch|template|this|throw|true|try|typedef|typeid|typename|union|unsigned"</span>
299   <span class="string">"|using|virtual|void|volatile|wchar_t|while)\\&gt;"</span>
300   <span class="special">;</span>
301
302<span class="keyword">const</span> <span class="keyword">char</span><span class="special">*</span> <span class="identifier">format_string</span> <span class="special">=</span> <span class="string">"(?1&lt;font color=\"#008040\"&gt;$&amp;&lt;/font&gt;)"</span>
303                           <span class="string">"(?2&lt;I&gt;&lt;font color=\"#000080\"&gt;$&amp;&lt;/font&gt;&lt;/I&gt;)"</span>
304                           <span class="string">"(?3&lt;font color=\"#0000A0\"&gt;$&amp;&lt;/font&gt;)"</span>
305                           <span class="string">"(?4&lt;font color=\"#0000FF\"&gt;$&amp;&lt;/font&gt;)"</span>
306                           <span class="string">"(?5&lt;B&gt;$&amp;&lt;/B&gt;)"</span><span class="special">;</span>
307
308<span class="keyword">const</span> <span class="keyword">char</span><span class="special">*</span> <span class="identifier">header_text</span> <span class="special">=</span>
309   <span class="string">"&lt;HTML&gt;\n&lt;HEAD&gt;\n"</span>
310   <span class="string">"&lt;TITLE&gt;Auto-generated html formatted source&lt;/TITLE&gt;\n"</span>
311   <span class="string">"&lt;META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=windows-1252\"&gt;\n"</span>
312   <span class="string">"&lt;/HEAD&gt;\n"</span>
313   <span class="string">"&lt;BODY LINK=\"#0000ff\" VLINK=\"#800080\" BGCOLOR=\"#ffffff\"&gt;\n"</span>
314   <span class="string">"&lt;P&gt; &lt;/P&gt;\n&lt;PRE&gt;"</span><span class="special">;</span>
315
316<span class="keyword">const</span> <span class="keyword">char</span><span class="special">*</span> <span class="identifier">footer_text</span> <span class="special">=</span> <span class="string">"&lt;/PRE&gt;\n&lt;/BODY&gt;\n\n"</span><span class="special">;</span>
317</pre>
318</div>
319<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
320<td align="left"></td>
321<td align="right"><div class="copyright-footer">Copyright © 1998-2013 John Maddock<p>
322        Distributed under the Boost Software License, Version 1.0. (See accompanying
323        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>)
324      </p>
325</div></td>
326</tr></table>
327<hr>
328<div class="spirit-nav">
329<a accesskey="p" href="regex_search.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../ref.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="regex_iterator.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
330</div>
331</body>
332</html>
333