Chris@16
|
1 // Boost string_algo library regex.hpp header file ---------------------------//
|
Chris@16
|
2
|
Chris@16
|
3 // Copyright Pavol Droba 2002-2003.
|
Chris@16
|
4 //
|
Chris@16
|
5 // Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
6 // (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
7 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
8
|
Chris@16
|
9 // See http://www.boost.org/ for updates, documentation, and revision history.
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_STRING_REGEX_HPP
|
Chris@16
|
12 #define BOOST_STRING_REGEX_HPP
|
Chris@16
|
13
|
Chris@16
|
14 #include <boost/algorithm/string/config.hpp>
|
Chris@16
|
15 #include <boost/regex.hpp>
|
Chris@16
|
16
|
Chris@16
|
17 #include <boost/range/iterator_range_core.hpp>
|
Chris@16
|
18 #include <boost/range/begin.hpp>
|
Chris@16
|
19 #include <boost/range/end.hpp>
|
Chris@16
|
20 #include <boost/range/iterator.hpp>
|
Chris@16
|
21 #include <boost/range/as_literal.hpp>
|
Chris@16
|
22
|
Chris@16
|
23 #include <boost/algorithm/string/find_format.hpp>
|
Chris@16
|
24 #include <boost/algorithm/string/regex_find_format.hpp>
|
Chris@16
|
25 #include <boost/algorithm/string/formatter.hpp>
|
Chris@16
|
26 #include <boost/algorithm/string/iter_find.hpp>
|
Chris@16
|
27
|
Chris@16
|
28 /*! \file
|
Chris@16
|
29 Defines regex variants of the algorithms.
|
Chris@16
|
30 */
|
Chris@16
|
31
|
Chris@16
|
32 namespace boost {
|
Chris@16
|
33 namespace algorithm {
|
Chris@16
|
34
|
Chris@16
|
35 // find_regex -----------------------------------------------//
|
Chris@16
|
36
|
Chris@16
|
37 //! Find regex algorithm
|
Chris@16
|
38 /*!
|
Chris@16
|
39 Search for a substring matching the given regex in the input.
|
Chris@16
|
40
|
Chris@16
|
41 \param Input A container which will be searched.
|
Chris@16
|
42 \param Rx A regular expression
|
Chris@16
|
43 \param Flags Regex options
|
Chris@16
|
44 \return
|
Chris@16
|
45 An \c iterator_range delimiting the match.
|
Chris@16
|
46 Returned iterator is either \c RangeT::iterator or
|
Chris@16
|
47 \c RangeT::const_iterator, depending on the constness of
|
Chris@16
|
48 the input parameter.
|
Chris@16
|
49
|
Chris@16
|
50 \note This function provides the strong exception-safety guarantee
|
Chris@16
|
51 */
|
Chris@16
|
52 template<
|
Chris@16
|
53 typename RangeT,
|
Chris@16
|
54 typename CharT,
|
Chris@16
|
55 typename RegexTraitsT>
|
Chris@16
|
56 inline iterator_range<
|
Chris@16
|
57 BOOST_STRING_TYPENAME range_iterator<RangeT>::type >
|
Chris@16
|
58 find_regex(
|
Chris@16
|
59 RangeT& Input,
|
Chris@16
|
60 const basic_regex<CharT, RegexTraitsT>& Rx,
|
Chris@16
|
61 match_flag_type Flags=match_default )
|
Chris@16
|
62 {
|
Chris@16
|
63 iterator_range<BOOST_STRING_TYPENAME range_iterator<RangeT>::type> lit_input(::boost::as_literal(Input));
|
Chris@16
|
64
|
Chris@16
|
65 return ::boost::algorithm::regex_finder(Rx,Flags)(
|
Chris@16
|
66 ::boost::begin(lit_input), ::boost::end(lit_input) );
|
Chris@16
|
67 }
|
Chris@16
|
68
|
Chris@16
|
69 // replace_regex --------------------------------------------------------------------//
|
Chris@16
|
70
|
Chris@16
|
71 //! Replace regex algorithm
|
Chris@16
|
72 /*!
|
Chris@16
|
73 Search for a substring matching given regex and format it with
|
Chris@16
|
74 the specified format.
|
Chris@16
|
75 The result is a modified copy of the input. It is returned as a sequence
|
Chris@16
|
76 or copied to the output iterator.
|
Chris@16
|
77
|
Chris@16
|
78 \param Output An output iterator to which the result will be copied
|
Chris@16
|
79 \param Input An input string
|
Chris@16
|
80 \param Rx A regular expression
|
Chris@16
|
81 \param Format Regex format definition
|
Chris@16
|
82 \param Flags Regex options
|
Chris@16
|
83 \return An output iterator pointing just after the last inserted character or
|
Chris@16
|
84 a modified copy of the input
|
Chris@16
|
85
|
Chris@16
|
86 \note The second variant of this function provides the strong exception-safety guarantee
|
Chris@16
|
87 */
|
Chris@16
|
88 template<
|
Chris@16
|
89 typename OutputIteratorT,
|
Chris@16
|
90 typename RangeT,
|
Chris@16
|
91 typename CharT,
|
Chris@16
|
92 typename RegexTraitsT,
|
Chris@16
|
93 typename FormatStringTraitsT, typename FormatStringAllocatorT >
|
Chris@16
|
94 inline OutputIteratorT replace_regex_copy(
|
Chris@16
|
95 OutputIteratorT Output,
|
Chris@16
|
96 const RangeT& Input,
|
Chris@16
|
97 const basic_regex<CharT, RegexTraitsT>& Rx,
|
Chris@16
|
98 const std::basic_string<CharT, FormatStringTraitsT, FormatStringAllocatorT>& Format,
|
Chris@16
|
99 match_flag_type Flags=match_default | format_default )
|
Chris@16
|
100 {
|
Chris@16
|
101 return ::boost::algorithm::find_format_copy(
|
Chris@16
|
102 Output,
|
Chris@16
|
103 Input,
|
Chris@16
|
104 ::boost::algorithm::regex_finder( Rx, Flags ),
|
Chris@16
|
105 ::boost::algorithm::regex_formatter( Format, Flags ) );
|
Chris@16
|
106 }
|
Chris@16
|
107
|
Chris@16
|
108 //! Replace regex algorithm
|
Chris@16
|
109 /*!
|
Chris@16
|
110 \overload
|
Chris@16
|
111 */
|
Chris@16
|
112 template<
|
Chris@16
|
113 typename SequenceT,
|
Chris@16
|
114 typename CharT,
|
Chris@16
|
115 typename RegexTraitsT,
|
Chris@16
|
116 typename FormatStringTraitsT, typename FormatStringAllocatorT >
|
Chris@16
|
117 inline SequenceT replace_regex_copy(
|
Chris@16
|
118 const SequenceT& Input,
|
Chris@16
|
119 const basic_regex<CharT, RegexTraitsT>& Rx,
|
Chris@16
|
120 const std::basic_string<CharT, FormatStringTraitsT, FormatStringAllocatorT>& Format,
|
Chris@16
|
121 match_flag_type Flags=match_default | format_default )
|
Chris@16
|
122 {
|
Chris@16
|
123 return ::boost::algorithm::find_format_copy(
|
Chris@16
|
124 Input,
|
Chris@16
|
125 ::boost::algorithm::regex_finder( Rx, Flags ),
|
Chris@16
|
126 ::boost::algorithm::regex_formatter( Format, Flags ) );
|
Chris@16
|
127 }
|
Chris@16
|
128
|
Chris@16
|
129 //! Replace regex algorithm
|
Chris@16
|
130 /*!
|
Chris@16
|
131 Search for a substring matching given regex and format it with
|
Chris@16
|
132 the specified format. The input string is modified in-place.
|
Chris@16
|
133
|
Chris@16
|
134 \param Input An input string
|
Chris@16
|
135 \param Rx A regular expression
|
Chris@16
|
136 \param Format Regex format definition
|
Chris@16
|
137 \param Flags Regex options
|
Chris@16
|
138 */
|
Chris@16
|
139 template<
|
Chris@16
|
140 typename SequenceT,
|
Chris@16
|
141 typename CharT,
|
Chris@16
|
142 typename RegexTraitsT,
|
Chris@16
|
143 typename FormatStringTraitsT, typename FormatStringAllocatorT >
|
Chris@16
|
144 inline void replace_regex(
|
Chris@16
|
145 SequenceT& Input,
|
Chris@16
|
146 const basic_regex<CharT, RegexTraitsT>& Rx,
|
Chris@16
|
147 const std::basic_string<CharT, FormatStringTraitsT, FormatStringAllocatorT>& Format,
|
Chris@16
|
148 match_flag_type Flags=match_default | format_default )
|
Chris@16
|
149 {
|
Chris@16
|
150 ::boost::algorithm::find_format(
|
Chris@16
|
151 Input,
|
Chris@16
|
152 ::boost::algorithm::regex_finder( Rx, Flags ),
|
Chris@16
|
153 ::boost::algorithm::regex_formatter( Format, Flags ) );
|
Chris@16
|
154 }
|
Chris@16
|
155
|
Chris@16
|
156 // replace_all_regex --------------------------------------------------------------------//
|
Chris@16
|
157
|
Chris@16
|
158 //! Replace all regex algorithm
|
Chris@16
|
159 /*!
|
Chris@16
|
160 Format all substrings, matching given regex, with the specified format.
|
Chris@16
|
161 The result is a modified copy of the input. It is returned as a sequence
|
Chris@16
|
162 or copied to the output iterator.
|
Chris@16
|
163
|
Chris@16
|
164 \param Output An output iterator to which the result will be copied
|
Chris@16
|
165 \param Input An input string
|
Chris@16
|
166 \param Rx A regular expression
|
Chris@16
|
167 \param Format Regex format definition
|
Chris@16
|
168 \param Flags Regex options
|
Chris@16
|
169 \return An output iterator pointing just after the last inserted character or
|
Chris@16
|
170 a modified copy of the input
|
Chris@16
|
171
|
Chris@16
|
172 \note The second variant of this function provides the strong exception-safety guarantee
|
Chris@16
|
173 */
|
Chris@16
|
174 template<
|
Chris@16
|
175 typename OutputIteratorT,
|
Chris@16
|
176 typename RangeT,
|
Chris@16
|
177 typename CharT,
|
Chris@16
|
178 typename RegexTraitsT,
|
Chris@16
|
179 typename FormatStringTraitsT, typename FormatStringAllocatorT >
|
Chris@16
|
180 inline OutputIteratorT replace_all_regex_copy(
|
Chris@16
|
181 OutputIteratorT Output,
|
Chris@16
|
182 const RangeT& Input,
|
Chris@16
|
183 const basic_regex<CharT, RegexTraitsT>& Rx,
|
Chris@16
|
184 const std::basic_string<CharT, FormatStringTraitsT, FormatStringAllocatorT>& Format,
|
Chris@16
|
185 match_flag_type Flags=match_default | format_default )
|
Chris@16
|
186 {
|
Chris@16
|
187 return ::boost::algorithm::find_format_all_copy(
|
Chris@16
|
188 Output,
|
Chris@16
|
189 Input,
|
Chris@16
|
190 ::boost::algorithm::regex_finder( Rx, Flags ),
|
Chris@16
|
191 ::boost::algorithm::regex_formatter( Format, Flags ) );
|
Chris@16
|
192 }
|
Chris@16
|
193
|
Chris@16
|
194 //! Replace all regex algorithm
|
Chris@16
|
195 /*!
|
Chris@16
|
196 \overload
|
Chris@16
|
197 */
|
Chris@16
|
198 template<
|
Chris@16
|
199 typename SequenceT,
|
Chris@16
|
200 typename CharT,
|
Chris@16
|
201 typename RegexTraitsT,
|
Chris@16
|
202 typename FormatStringTraitsT, typename FormatStringAllocatorT >
|
Chris@16
|
203 inline SequenceT replace_all_regex_copy(
|
Chris@16
|
204 const SequenceT& Input,
|
Chris@16
|
205 const basic_regex<CharT, RegexTraitsT>& Rx,
|
Chris@16
|
206 const std::basic_string<CharT, FormatStringTraitsT, FormatStringAllocatorT>& Format,
|
Chris@16
|
207 match_flag_type Flags=match_default | format_default )
|
Chris@16
|
208 {
|
Chris@16
|
209 return ::boost::algorithm::find_format_all_copy(
|
Chris@16
|
210 Input,
|
Chris@16
|
211 ::boost::algorithm::regex_finder( Rx, Flags ),
|
Chris@16
|
212 ::boost::algorithm::regex_formatter( Format, Flags ) );
|
Chris@16
|
213 }
|
Chris@16
|
214
|
Chris@16
|
215 //! Replace all regex algorithm
|
Chris@16
|
216 /*!
|
Chris@16
|
217 Format all substrings, matching given regex, with the specified format.
|
Chris@16
|
218 The input string is modified in-place.
|
Chris@16
|
219
|
Chris@16
|
220 \param Input An input string
|
Chris@16
|
221 \param Rx A regular expression
|
Chris@16
|
222 \param Format Regex format definition
|
Chris@16
|
223 \param Flags Regex options
|
Chris@16
|
224 */
|
Chris@16
|
225 template<
|
Chris@16
|
226 typename SequenceT,
|
Chris@16
|
227 typename CharT,
|
Chris@16
|
228 typename RegexTraitsT,
|
Chris@16
|
229 typename FormatStringTraitsT, typename FormatStringAllocatorT >
|
Chris@16
|
230 inline void replace_all_regex(
|
Chris@16
|
231 SequenceT& Input,
|
Chris@16
|
232 const basic_regex<CharT, RegexTraitsT>& Rx,
|
Chris@16
|
233 const std::basic_string<CharT, FormatStringTraitsT, FormatStringAllocatorT>& Format,
|
Chris@16
|
234 match_flag_type Flags=match_default | format_default )
|
Chris@16
|
235 {
|
Chris@16
|
236 ::boost::algorithm::find_format_all(
|
Chris@16
|
237 Input,
|
Chris@16
|
238 ::boost::algorithm::regex_finder( Rx, Flags ),
|
Chris@16
|
239 ::boost::algorithm::regex_formatter( Format, Flags ) );
|
Chris@16
|
240 }
|
Chris@16
|
241
|
Chris@16
|
242 // erase_regex --------------------------------------------------------------------//
|
Chris@16
|
243
|
Chris@16
|
244 //! Erase regex algorithm
|
Chris@16
|
245 /*!
|
Chris@16
|
246 Remove a substring matching given regex from the input.
|
Chris@16
|
247 The result is a modified copy of the input. It is returned as a sequence
|
Chris@16
|
248 or copied to the output iterator.
|
Chris@16
|
249
|
Chris@16
|
250 \param Output An output iterator to which the result will be copied
|
Chris@16
|
251 \param Input An input string
|
Chris@16
|
252 \param Rx A regular expression
|
Chris@16
|
253 \param Flags Regex options
|
Chris@16
|
254 \return An output iterator pointing just after the last inserted character or
|
Chris@16
|
255 a modified copy of the input
|
Chris@16
|
256
|
Chris@16
|
257 \note The second variant of this function provides the strong exception-safety guarantee
|
Chris@16
|
258 */
|
Chris@16
|
259 template<
|
Chris@16
|
260 typename OutputIteratorT,
|
Chris@16
|
261 typename RangeT,
|
Chris@16
|
262 typename CharT,
|
Chris@16
|
263 typename RegexTraitsT >
|
Chris@16
|
264 inline OutputIteratorT erase_regex_copy(
|
Chris@16
|
265 OutputIteratorT Output,
|
Chris@16
|
266 const RangeT& Input,
|
Chris@16
|
267 const basic_regex<CharT, RegexTraitsT>& Rx,
|
Chris@16
|
268 match_flag_type Flags=match_default )
|
Chris@16
|
269 {
|
Chris@16
|
270 return ::boost::algorithm::find_format_copy(
|
Chris@16
|
271 Output,
|
Chris@16
|
272 Input,
|
Chris@16
|
273 ::boost::algorithm::regex_finder( Rx, Flags ),
|
Chris@16
|
274 ::boost::algorithm::empty_formatter( Input ) );
|
Chris@16
|
275 }
|
Chris@16
|
276
|
Chris@16
|
277 //! Erase regex algorithm
|
Chris@16
|
278 /*!
|
Chris@16
|
279 \overload
|
Chris@16
|
280 */
|
Chris@16
|
281 template<
|
Chris@16
|
282 typename SequenceT,
|
Chris@16
|
283 typename CharT,
|
Chris@16
|
284 typename RegexTraitsT >
|
Chris@16
|
285 inline SequenceT erase_regex_copy(
|
Chris@16
|
286 const SequenceT& Input,
|
Chris@16
|
287 const basic_regex<CharT, RegexTraitsT>& Rx,
|
Chris@16
|
288 match_flag_type Flags=match_default )
|
Chris@16
|
289 {
|
Chris@16
|
290 return ::boost::algorithm::find_format_copy(
|
Chris@16
|
291 Input,
|
Chris@16
|
292 ::boost::algorithm::regex_finder( Rx, Flags ),
|
Chris@16
|
293 ::boost::algorithm::empty_formatter( Input ) );
|
Chris@16
|
294 }
|
Chris@16
|
295
|
Chris@16
|
296 //! Erase regex algorithm
|
Chris@16
|
297 /*!
|
Chris@16
|
298 Remove a substring matching given regex from the input.
|
Chris@16
|
299 The input string is modified in-place.
|
Chris@16
|
300
|
Chris@16
|
301 \param Input An input string
|
Chris@16
|
302 \param Rx A regular expression
|
Chris@16
|
303 \param Flags Regex options
|
Chris@16
|
304 */
|
Chris@16
|
305 template<
|
Chris@16
|
306 typename SequenceT,
|
Chris@16
|
307 typename CharT,
|
Chris@16
|
308 typename RegexTraitsT >
|
Chris@16
|
309 inline void erase_regex(
|
Chris@16
|
310 SequenceT& Input,
|
Chris@16
|
311 const basic_regex<CharT, RegexTraitsT>& Rx,
|
Chris@16
|
312 match_flag_type Flags=match_default )
|
Chris@16
|
313 {
|
Chris@16
|
314 ::boost::algorithm::find_format(
|
Chris@16
|
315 Input,
|
Chris@16
|
316 ::boost::algorithm::regex_finder( Rx, Flags ),
|
Chris@16
|
317 ::boost::algorithm::empty_formatter( Input ) );
|
Chris@16
|
318 }
|
Chris@16
|
319
|
Chris@16
|
320 // erase_all_regex --------------------------------------------------------------------//
|
Chris@16
|
321
|
Chris@16
|
322 //! Erase all regex algorithm
|
Chris@16
|
323 /*!
|
Chris@16
|
324 Erase all substrings, matching given regex, from the input.
|
Chris@16
|
325 The result is a modified copy of the input. It is returned as a sequence
|
Chris@16
|
326 or copied to the output iterator.
|
Chris@16
|
327
|
Chris@16
|
328
|
Chris@16
|
329 \param Output An output iterator to which the result will be copied
|
Chris@16
|
330 \param Input An input string
|
Chris@16
|
331 \param Rx A regular expression
|
Chris@16
|
332 \param Flags Regex options
|
Chris@16
|
333 \return An output iterator pointing just after the last inserted character or
|
Chris@16
|
334 a modified copy of the input
|
Chris@16
|
335
|
Chris@16
|
336 \note The second variant of this function provides the strong exception-safety guarantee
|
Chris@16
|
337 */
|
Chris@16
|
338 template<
|
Chris@16
|
339 typename OutputIteratorT,
|
Chris@16
|
340 typename RangeT,
|
Chris@16
|
341 typename CharT,
|
Chris@16
|
342 typename RegexTraitsT >
|
Chris@16
|
343 inline OutputIteratorT erase_all_regex_copy(
|
Chris@16
|
344 OutputIteratorT Output,
|
Chris@16
|
345 const RangeT& Input,
|
Chris@16
|
346 const basic_regex<CharT, RegexTraitsT>& Rx,
|
Chris@16
|
347 match_flag_type Flags=match_default )
|
Chris@16
|
348 {
|
Chris@16
|
349 return ::boost::algorithm::find_format_all_copy(
|
Chris@16
|
350 Output,
|
Chris@16
|
351 Input,
|
Chris@16
|
352 ::boost::algorithm::regex_finder( Rx, Flags ),
|
Chris@16
|
353 ::boost::algorithm::empty_formatter( Input ) );
|
Chris@16
|
354 }
|
Chris@16
|
355
|
Chris@16
|
356 //! Erase all regex algorithm
|
Chris@16
|
357 /*!
|
Chris@16
|
358 \overload
|
Chris@16
|
359 */
|
Chris@16
|
360 template<
|
Chris@16
|
361 typename SequenceT,
|
Chris@16
|
362 typename CharT,
|
Chris@16
|
363 typename RegexTraitsT >
|
Chris@16
|
364 inline SequenceT erase_all_regex_copy(
|
Chris@16
|
365 const SequenceT& Input,
|
Chris@16
|
366 const basic_regex<CharT, RegexTraitsT>& Rx,
|
Chris@16
|
367 match_flag_type Flags=match_default )
|
Chris@16
|
368 {
|
Chris@16
|
369 return ::boost::algorithm::find_format_all_copy(
|
Chris@16
|
370 Input,
|
Chris@16
|
371 ::boost::algorithm::regex_finder( Rx, Flags ),
|
Chris@16
|
372 ::boost::algorithm::empty_formatter( Input ) );
|
Chris@16
|
373 }
|
Chris@16
|
374
|
Chris@16
|
375 //! Erase all regex algorithm
|
Chris@16
|
376 /*!
|
Chris@16
|
377 Erase all substrings, matching given regex, from the input.
|
Chris@16
|
378 The input string is modified in-place.
|
Chris@16
|
379
|
Chris@16
|
380 \param Input An input string
|
Chris@16
|
381 \param Rx A regular expression
|
Chris@16
|
382 \param Flags Regex options
|
Chris@16
|
383 */
|
Chris@16
|
384 template<
|
Chris@16
|
385 typename SequenceT,
|
Chris@16
|
386 typename CharT,
|
Chris@16
|
387 typename RegexTraitsT>
|
Chris@16
|
388 inline void erase_all_regex(
|
Chris@16
|
389 SequenceT& Input,
|
Chris@16
|
390 const basic_regex<CharT, RegexTraitsT>& Rx,
|
Chris@16
|
391 match_flag_type Flags=match_default )
|
Chris@16
|
392 {
|
Chris@16
|
393 ::boost::algorithm::find_format_all(
|
Chris@16
|
394 Input,
|
Chris@16
|
395 ::boost::algorithm::regex_finder( Rx, Flags ),
|
Chris@16
|
396 ::boost::algorithm::empty_formatter( Input ) );
|
Chris@16
|
397 }
|
Chris@16
|
398
|
Chris@16
|
399 // find_all_regex ------------------------------------------------------------------//
|
Chris@16
|
400
|
Chris@16
|
401 //! Find all regex algorithm
|
Chris@16
|
402 /*!
|
Chris@16
|
403 This algorithm finds all substrings matching the give regex
|
Chris@16
|
404 in the input.
|
Chris@16
|
405
|
Chris@16
|
406 Each part is copied and added as a new element to the output container.
|
Chris@16
|
407 Thus the result container must be able to hold copies
|
Chris@16
|
408 of the matches (in a compatible structure like std::string) or
|
Chris@16
|
409 a reference to it (e.g. using the iterator range class).
|
Chris@16
|
410 Examples of such a container are \c std::vector<std::string>
|
Chris@16
|
411 or \c std::list<boost::iterator_range<std::string::iterator>>
|
Chris@16
|
412
|
Chris@16
|
413 \param Result A container that can hold copies of references to the substrings.
|
Chris@16
|
414 \param Input A container which will be searched.
|
Chris@16
|
415 \param Rx A regular expression
|
Chris@16
|
416 \param Flags Regex options
|
Chris@16
|
417 \return A reference to the result
|
Chris@16
|
418
|
Chris@16
|
419 \note Prior content of the result will be overwritten.
|
Chris@16
|
420
|
Chris@16
|
421 \note This function provides the strong exception-safety guarantee
|
Chris@16
|
422 */
|
Chris@16
|
423 template<
|
Chris@16
|
424 typename SequenceSequenceT,
|
Chris@16
|
425 typename RangeT,
|
Chris@16
|
426 typename CharT,
|
Chris@16
|
427 typename RegexTraitsT >
|
Chris@16
|
428 inline SequenceSequenceT& find_all_regex(
|
Chris@16
|
429 SequenceSequenceT& Result,
|
Chris@16
|
430 const RangeT& Input,
|
Chris@16
|
431 const basic_regex<CharT, RegexTraitsT>& Rx,
|
Chris@16
|
432 match_flag_type Flags=match_default )
|
Chris@16
|
433 {
|
Chris@16
|
434 return ::boost::algorithm::iter_find(
|
Chris@16
|
435 Result,
|
Chris@16
|
436 Input,
|
Chris@16
|
437 ::boost::algorithm::regex_finder(Rx,Flags) );
|
Chris@16
|
438 }
|
Chris@16
|
439
|
Chris@16
|
440 // split_regex ------------------------------------------------------------------//
|
Chris@16
|
441
|
Chris@16
|
442 //! Split regex algorithm
|
Chris@16
|
443 /*!
|
Chris@16
|
444 Tokenize expression. This function is equivalent to C strtok. Input
|
Chris@16
|
445 sequence is split into tokens, separated by separators. Separator
|
Chris@16
|
446 is an every match of the given regex.
|
Chris@16
|
447 Each part is copied and added as a new element to the output container.
|
Chris@16
|
448 Thus the result container must be able to hold copies
|
Chris@16
|
449 of the matches (in a compatible structure like std::string) or
|
Chris@16
|
450 a reference to it (e.g. using the iterator range class).
|
Chris@16
|
451 Examples of such a container are \c std::vector<std::string>
|
Chris@16
|
452 or \c std::list<boost::iterator_range<std::string::iterator>>
|
Chris@16
|
453
|
Chris@16
|
454 \param Result A container that can hold copies of references to the substrings.
|
Chris@16
|
455 \param Input A container which will be searched.
|
Chris@16
|
456 \param Rx A regular expression
|
Chris@16
|
457 \param Flags Regex options
|
Chris@16
|
458 \return A reference to the result
|
Chris@16
|
459
|
Chris@16
|
460 \note Prior content of the result will be overwritten.
|
Chris@16
|
461
|
Chris@16
|
462 \note This function provides the strong exception-safety guarantee
|
Chris@16
|
463 */
|
Chris@16
|
464 template<
|
Chris@16
|
465 typename SequenceSequenceT,
|
Chris@16
|
466 typename RangeT,
|
Chris@16
|
467 typename CharT,
|
Chris@16
|
468 typename RegexTraitsT >
|
Chris@16
|
469 inline SequenceSequenceT& split_regex(
|
Chris@16
|
470 SequenceSequenceT& Result,
|
Chris@16
|
471 const RangeT& Input,
|
Chris@16
|
472 const basic_regex<CharT, RegexTraitsT>& Rx,
|
Chris@16
|
473 match_flag_type Flags=match_default )
|
Chris@16
|
474 {
|
Chris@16
|
475 return ::boost::algorithm::iter_split(
|
Chris@16
|
476 Result,
|
Chris@16
|
477 Input,
|
Chris@16
|
478 ::boost::algorithm::regex_finder(Rx,Flags) );
|
Chris@16
|
479 }
|
Chris@16
|
480
|
Chris@16
|
481 // join_if ------------------------------------------------------------------//
|
Chris@16
|
482
|
Chris@16
|
483 #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
|
Chris@16
|
484
|
Chris@16
|
485 //! Conditional join algorithm
|
Chris@16
|
486 /*!
|
Chris@16
|
487 This algorithm joins all strings in a 'list' into one long string.
|
Chris@16
|
488 Segments are concatenated by given separator. Only segments that
|
Chris@16
|
489 match the given regular expression will be added to the result
|
Chris@16
|
490
|
Chris@16
|
491 This is a specialization of join_if algorithm.
|
Chris@16
|
492
|
Chris@16
|
493 \param Input A container that holds the input strings. It must be a container-of-containers.
|
Chris@16
|
494 \param Separator A string that will separate the joined segments.
|
Chris@16
|
495 \param Rx A regular expression
|
Chris@16
|
496 \param Flags Regex options
|
Chris@16
|
497 \return Concatenated string.
|
Chris@16
|
498
|
Chris@16
|
499 \note This function provides the strong exception-safety guarantee
|
Chris@16
|
500 */
|
Chris@16
|
501 template<
|
Chris@16
|
502 typename SequenceSequenceT,
|
Chris@16
|
503 typename Range1T,
|
Chris@16
|
504 typename CharT,
|
Chris@16
|
505 typename RegexTraitsT >
|
Chris@16
|
506 inline typename range_value<SequenceSequenceT>::type
|
Chris@16
|
507 join_if(
|
Chris@16
|
508 const SequenceSequenceT& Input,
|
Chris@16
|
509 const Range1T& Separator,
|
Chris@16
|
510 const basic_regex<CharT, RegexTraitsT>& Rx,
|
Chris@16
|
511 match_flag_type Flags=match_default )
|
Chris@16
|
512 {
|
Chris@16
|
513 // Define working types
|
Chris@16
|
514 typedef typename range_value<SequenceSequenceT>::type ResultT;
|
Chris@16
|
515 typedef typename range_const_iterator<SequenceSequenceT>::type InputIteratorT;
|
Chris@16
|
516
|
Chris@16
|
517 // Parse input
|
Chris@16
|
518 InputIteratorT itBegin=::boost::begin(Input);
|
Chris@16
|
519 InputIteratorT itEnd=::boost::end(Input);
|
Chris@16
|
520
|
Chris@16
|
521 // Construct container to hold the result
|
Chris@16
|
522 ResultT Result;
|
Chris@16
|
523
|
Chris@16
|
524
|
Chris@16
|
525 // Roll to the first element that will be added
|
Chris@16
|
526 while(
|
Chris@16
|
527 itBegin!=itEnd &&
|
Chris@16
|
528 !::boost::regex_match(::boost::begin(*itBegin), ::boost::end(*itBegin), Rx, Flags)) ++itBegin;
|
Chris@16
|
529
|
Chris@16
|
530 // Add this element
|
Chris@16
|
531 if(itBegin!=itEnd)
|
Chris@16
|
532 {
|
Chris@16
|
533 detail::insert(Result, ::boost::end(Result), *itBegin);
|
Chris@16
|
534 ++itBegin;
|
Chris@16
|
535 }
|
Chris@16
|
536
|
Chris@16
|
537 for(;itBegin!=itEnd; ++itBegin)
|
Chris@16
|
538 {
|
Chris@16
|
539 if(::boost::regex_match(::boost::begin(*itBegin), ::boost::end(*itBegin), Rx, Flags))
|
Chris@16
|
540 {
|
Chris@16
|
541 // Add separator
|
Chris@16
|
542 detail::insert(Result, ::boost::end(Result), ::boost::as_literal(Separator));
|
Chris@16
|
543 // Add element
|
Chris@16
|
544 detail::insert(Result, ::boost::end(Result), *itBegin);
|
Chris@16
|
545 }
|
Chris@16
|
546 }
|
Chris@16
|
547
|
Chris@16
|
548 return Result;
|
Chris@16
|
549 }
|
Chris@16
|
550
|
Chris@16
|
551 #else // BOOST_NO_FUNCTION_TEMPLATE_ORDERING
|
Chris@16
|
552
|
Chris@16
|
553 //! Conditional join algorithm
|
Chris@16
|
554 /*!
|
Chris@16
|
555 This algorithm joins all strings in a 'list' into one long string.
|
Chris@16
|
556 Segments are concatenated by given separator. Only segments that
|
Chris@16
|
557 match the given regular expression will be added to the result
|
Chris@16
|
558
|
Chris@16
|
559 This is a specialization of join_if algorithm.
|
Chris@16
|
560
|
Chris@16
|
561 \param Input A container that holds the input strings. It must be a container-of-containers.
|
Chris@16
|
562 \param Separator A string that will separate the joined segments.
|
Chris@16
|
563 \param Rx A regular expression
|
Chris@16
|
564 \param Flags Regex options
|
Chris@16
|
565 \return Concatenated string.
|
Chris@16
|
566
|
Chris@16
|
567 \note This function provides the strong exception-safety guarantee
|
Chris@16
|
568 */
|
Chris@16
|
569 template<
|
Chris@16
|
570 typename SequenceSequenceT,
|
Chris@16
|
571 typename Range1T,
|
Chris@16
|
572 typename CharT,
|
Chris@16
|
573 typename RegexTraitsT >
|
Chris@16
|
574 inline typename range_value<SequenceSequenceT>::type
|
Chris@16
|
575 join_if_regex(
|
Chris@16
|
576 const SequenceSequenceT& Input,
|
Chris@16
|
577 const Range1T& Separator,
|
Chris@16
|
578 const basic_regex<CharT, RegexTraitsT>& Rx,
|
Chris@16
|
579 match_flag_type Flags=match_default )
|
Chris@16
|
580 {
|
Chris@16
|
581 // Define working types
|
Chris@16
|
582 typedef typename range_value<SequenceSequenceT>::type ResultT;
|
Chris@16
|
583 typedef typename range_const_iterator<SequenceSequenceT>::type InputIteratorT;
|
Chris@16
|
584
|
Chris@16
|
585 // Parse input
|
Chris@16
|
586 InputIteratorT itBegin=::boost::begin(Input);
|
Chris@16
|
587 InputIteratorT itEnd=::boost::end(Input);
|
Chris@16
|
588
|
Chris@16
|
589 // Construct container to hold the result
|
Chris@16
|
590 ResultT Result;
|
Chris@16
|
591
|
Chris@16
|
592
|
Chris@16
|
593 // Roll to the first element that will be added
|
Chris@16
|
594 while(
|
Chris@16
|
595 itBegin!=itEnd &&
|
Chris@16
|
596 !::boost::regex_match(::boost::begin(*itBegin), ::boost::end(*itBegin), Rx, Flags)) ++itBegin;
|
Chris@16
|
597
|
Chris@16
|
598 // Add this element
|
Chris@16
|
599 if(itBegin!=itEnd)
|
Chris@16
|
600 {
|
Chris@16
|
601 detail::insert(Result, ::boost::end(Result), *itBegin);
|
Chris@16
|
602 ++itBegin;
|
Chris@16
|
603 }
|
Chris@16
|
604
|
Chris@16
|
605 for(;itBegin!=itEnd; ++itBegin)
|
Chris@16
|
606 {
|
Chris@16
|
607 if(::boost::regex_match(::boost::begin(*itBegin), ::boost::end(*itBegin), Rx, Flags))
|
Chris@16
|
608 {
|
Chris@16
|
609 // Add separator
|
Chris@16
|
610 detail::insert(Result, ::boost::end(Result), ::boost::as_literal(Separator));
|
Chris@16
|
611 // Add element
|
Chris@16
|
612 detail::insert(Result, ::boost::end(Result), *itBegin);
|
Chris@16
|
613 }
|
Chris@16
|
614 }
|
Chris@16
|
615
|
Chris@16
|
616 return Result;
|
Chris@16
|
617 }
|
Chris@16
|
618
|
Chris@16
|
619
|
Chris@16
|
620 #endif // BOOST_NO_FUNCTION_TEMPLATE_ORDERING
|
Chris@16
|
621
|
Chris@16
|
622 } // namespace algorithm
|
Chris@16
|
623
|
Chris@16
|
624 // pull names into the boost namespace
|
Chris@16
|
625 using algorithm::find_regex;
|
Chris@16
|
626 using algorithm::replace_regex;
|
Chris@16
|
627 using algorithm::replace_regex_copy;
|
Chris@16
|
628 using algorithm::replace_all_regex;
|
Chris@16
|
629 using algorithm::replace_all_regex_copy;
|
Chris@16
|
630 using algorithm::erase_regex;
|
Chris@16
|
631 using algorithm::erase_regex_copy;
|
Chris@16
|
632 using algorithm::erase_all_regex;
|
Chris@16
|
633 using algorithm::erase_all_regex_copy;
|
Chris@16
|
634 using algorithm::find_all_regex;
|
Chris@16
|
635 using algorithm::split_regex;
|
Chris@16
|
636
|
Chris@16
|
637 #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
|
Chris@16
|
638 using algorithm::join_if;
|
Chris@16
|
639 #else // BOOST_NO_FUNCTION_TEMPLATE_ORDERING
|
Chris@16
|
640 using algorithm::join_if_regex;
|
Chris@16
|
641 #endif // BOOST_NO_FUNCTION_TEMPLATE_ORDERING
|
Chris@16
|
642
|
Chris@16
|
643 } // namespace boost
|
Chris@16
|
644
|
Chris@16
|
645
|
Chris@16
|
646 #endif // BOOST_STRING_REGEX_HPP
|