• 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_split (deprecated)</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="../deprecated.html" title="Deprecated Interfaces">
9<link rel="prev" href="regex_grep.html" title="regex_grep (Deprecated)">
10<link rel="next" href="old_regex.html" title="High Level Class RegEx (Deprecated)">
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_grep.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../deprecated.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="old_regex.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h4 class="title">
27<a name="boost_regex.ref.deprecated.regex_split"></a><a class="link" href="regex_split.html" title="regex_split (deprecated)">regex_split
28        (deprecated)</a>
29</h4></div></div></div>
30<p>
31          The algorithm <a class="link" href="regex_split.html" title="regex_split (deprecated)"><code class="computeroutput"><span class="identifier">regex_split</span></code></a> has been deprecated
32          in favor of the iterator <a class="link" href="../regex_token_iterator.html" title="regex_token_iterator"><code class="computeroutput"><span class="identifier">regex_token_iterator</span></code></a> which has
33          a more flexible and powerful interface, as well as following the more usual
34          standard library "pull" rather than "push" semantics.
35        </p>
36<p>
37          Code which uses <a class="link" href="regex_split.html" title="regex_split (deprecated)"><code class="computeroutput"><span class="identifier">regex_split</span></code></a> will continue to compile,
38          the following documentation is taken from a previous Boost.Regex version:
39        </p>
40<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>
41</pre>
42<p>
43          Algorithm <a class="link" href="regex_split.html" title="regex_split (deprecated)"><code class="computeroutput"><span class="identifier">regex_split</span></code></a> performs a similar
44          operation to the perl split operation, and comes in three overloaded forms:
45        </p>
46<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">charT</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Traits1</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Alloc1</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Traits2</span><span class="special">&gt;</span>
47<span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">regex_split</span><span class="special">(</span><span class="identifier">OutputIterator</span> <span class="identifier">out</span><span class="special">,</span>
48                        <span class="identifier">std</span><span class="special">::</span><span class="identifier">basic_string</span><span class="special">&lt;</span><span class="identifier">charT</span><span class="special">,</span> <span class="identifier">Traits1</span><span class="special">,</span> <span class="identifier">Alloc1</span><span class="special">&gt;&amp;</span> <span class="identifier">s</span><span class="special">,</span>
49                        <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">Traits2</span><span class="special">&gt;&amp;</span> <span class="identifier">e</span><span class="special">,</span>
50                        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">match_flag_type</span> <span class="identifier">flags</span><span class="special">,</span>
51                        <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">max_split</span><span class="special">);</span>
52
53<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">charT</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Traits1</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Alloc1</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Traits2</span><span class="special">&gt;</span>
54<span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">regex_split</span><span class="special">(</span><span class="identifier">OutputIterator</span> <span class="identifier">out</span><span class="special">,</span>
55                        <span class="identifier">std</span><span class="special">::</span><span class="identifier">basic_string</span><span class="special">&lt;</span><span class="identifier">charT</span><span class="special">,</span> <span class="identifier">Traits1</span><span class="special">,</span> <span class="identifier">Alloc1</span><span class="special">&gt;&amp;</span> <span class="identifier">s</span><span class="special">,</span>
56                        <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">Traits2</span><span class="special">&gt;&amp;</span> <span class="identifier">e</span><span class="special">,</span>
57                        <span class="identifier">boost</span><span class="special">::</span><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>
58
59<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">charT</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Traits1</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Alloc1</span><span class="special">&gt;</span>
60<span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">regex_split</span><span class="special">(</span><span class="identifier">OutputIterator</span> <span class="identifier">out</span><span class="special">,</span>
61                        <span class="identifier">std</span><span class="special">::</span><span class="identifier">basic_string</span><span class="special">&lt;</span><span class="identifier">charT</span><span class="special">,</span> <span class="identifier">Traits1</span><span class="special">,</span> <span class="identifier">Alloc1</span><span class="special">&gt;&amp;</span> <span class="identifier">s</span><span class="special">);</span>
62</pre>
63<p>
64          <span class="bold"><strong>Effects</strong></span>: Each version of the algorithm
65          takes an output-iterator for output, and a string for input. If the expression
66          contains no marked sub-expressions, then the algorithm writes one string
67          onto the output-iterator for each section of input that does not match
68          the expression. If the expression does contain marked sub-expressions,
69          then each time a match is found, one string for each marked sub-expression
70          will be written to the output-iterator. No more than max_split strings
71          will be written to the output-iterator. Before returning, all the input
72          processed will be deleted from the string <span class="emphasis"><em>s</em></span> (if <span class="emphasis"><em>max_split</em></span>
73          is not reached then all of <span class="emphasis"><em>s</em></span> will be deleted). Returns
74          the number of strings written to the output-iterator. If the parameter
75          <span class="emphasis"><em>max_split</em></span> is not specified then it defaults to <code class="computeroutput"><span class="identifier">UINT_MAX</span></code>. If no expression is specified,
76          then it defaults to "\s+", and splitting occurs on whitespace.
77        </p>
78<p>
79          <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>
80          if the complexity of matching the expression against an N character string
81          begins to exceed O(N<sup>2</sup>), or if the program runs out of stack space while
82          matching the expression (if Boost.Regex is configured in recursive mode),
83          or if the matcher exhausts its permitted memory allocation (if Boost.Regex
84          is configured in non-recursive mode).
85        </p>
86<p>
87          <span class="bold"><strong>Example</strong></span>: the following function will split
88          the input string into a series of tokens, and remove each token from the
89          string <span class="emphasis"><em>s</em></span>:
90        </p>
91<pre class="programlisting"><span class="keyword">unsigned</span> <span class="identifier">tokenise</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">list</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">&gt;&amp;</span> <span class="identifier">l</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>
92<span class="special">{</span>
93   <span class="keyword">return</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">regex_split</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">back_inserter</span><span class="special">(</span><span class="identifier">l</span><span class="special">),</span> <span class="identifier">s</span><span class="special">);</span>
94<span class="special">}</span>
95</pre>
96<p>
97          Example: the following short program will extract all of the URL's from
98          a html file, and print them out to cout:
99        </p>
100<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">list</span><span class="special">&gt;</span>
101<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">fstream</span><span class="special">&gt;</span>
102<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>
103<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>
104
105<span class="identifier">boost</span><span class="special">::</span><span class="identifier">regex</span> <span class="identifier">e</span><span class="special">(</span><span class="string">"&lt;\\s*A\\s+[^&gt;]*href\\s*=\\s*\"([^\"]*)\""</span><span class="special">,</span>
106               <span class="identifier">boost</span><span class="special">::</span><span class="identifier">regbase</span><span class="special">::</span><span class="identifier">normal</span> <span class="special">|</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">regbase</span><span class="special">::</span><span class="identifier">icase</span><span class="special">);</span>
107
108<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>
109<span class="special">{</span>
110   <span class="identifier">s</span><span class="special">.</span><span class="identifier">erase</span><span class="special">();</span>
111   <span class="comment">//</span>
112   <span class="comment">// attempt to grow string buffer to match file size,</span>
113   <span class="comment">// this doesn't always work...</span>
114   <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">()-&amp;</span><span class="identifier">gtin_avail</span><span class="special">());</span>
115   <span class="keyword">char</span> <span class="identifier">c</span><span class="special">;</span>
116   <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>
117   <span class="special">{</span>
118      <span class="comment">// use logarithmic growth strategy, in case</span>
119      <span class="comment">// in_avail (above) returned zero:</span>
120      <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>
121         <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>
122      <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>
123   <span class="special">}</span>
124<span class="special">}</span>
125
126
127<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">char</span><span class="special">**</span> <span class="identifier">argv</span><span class="special">)</span>
128<span class="special">{</span>
129   <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">s</span><span class="special">;</span>
130   <span class="identifier">std</span><span class="special">::</span><span class="identifier">list</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">&gt;</span> <span class="identifier">l</span><span class="special">;</span>
131
132   <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>
133   <span class="special">{</span>
134      <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Findings URL's in "</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="string">":"</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>
135      <span class="identifier">s</span><span class="special">.</span><span class="identifier">erase</span><span class="special">();</span>
136      <span class="identifier">std</span><span class="special">::</span><span class="identifier">ifstream</span> <span class="identifier">is</span><span class="special">(</span><span class="identifier">argv</span><span class="special">[</span><span class="identifier">i</span><span class="special">]);</span>
137      <span class="identifier">load_file</span><span class="special">(</span><span class="identifier">s</span><span class="special">,</span> <span class="identifier">is</span><span class="special">);</span>
138      <span class="identifier">boost</span><span class="special">::</span><span class="identifier">regex_split</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">back_inserter</span><span class="special">(</span><span class="identifier">l</span><span class="special">),</span> <span class="identifier">s</span><span class="special">,</span> <span class="identifier">e</span><span class="special">);</span>
139      <span class="keyword">while</span><span class="special">(</span><span class="identifier">l</span><span class="special">.</span><span class="identifier">size</span><span class="special">())</span>
140      <span class="special">{</span>
141         <span class="identifier">s</span> <span class="special">=</span> <span class="special">*(</span><span class="identifier">l</span><span class="special">.</span><span class="identifier">begin</span><span class="special">());</span>
142         <span class="identifier">l</span><span class="special">.</span><span class="identifier">pop_front</span><span class="special">();</span>
143         <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">s</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>
144      <span class="special">}</span>
145   <span class="special">}</span>
146   <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
147<span class="special">}</span>
148</pre>
149</div>
150<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
151<td align="left"></td>
152<td align="right"><div class="copyright-footer">Copyright © 1998-2013 John Maddock<p>
153        Distributed under the Boost Software License, Version 1.0. (See accompanying
154        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>)
155      </p>
156</div></td>
157</tr></table>
158<hr>
159<div class="spirit-nav">
160<a accesskey="p" href="regex_grep.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../deprecated.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="old_regex.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
161</div>
162</body>
163</html>
164