1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
---|
2 | <html> |
---|
3 | <head> |
---|
4 | <title>Boost.Regex: Algorithm regex_replace</title> |
---|
5 | <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> |
---|
6 | <LINK href="../../../boost.css" type="text/css" rel="stylesheet"></head> |
---|
7 | <body> |
---|
8 | <P> |
---|
9 | <TABLE id="Table1" cellSpacing="1" cellPadding="1" width="100%" border="0"> |
---|
10 | <TR> |
---|
11 | <td vAlign="top" width="300"> |
---|
12 | <h3><A href="../../../index.htm"><IMG height="86" alt="C++ Boost" src="../../../boost.png" width="277" border="0"></A></h3> |
---|
13 | </td> |
---|
14 | <TD width="353"> |
---|
15 | <H1 align="center">Boost.Regex</H1> |
---|
16 | <H2 align="center">Algorithm regex_replace</H2> |
---|
17 | </TD> |
---|
18 | <td width="50"> |
---|
19 | <h3><A href="index.html"><IMG height="45" alt="Boost.Regex Index" src="uarrow.gif" width="43" border="0"></A></h3> |
---|
20 | </td> |
---|
21 | </TR> |
---|
22 | </TABLE> |
---|
23 | </P> |
---|
24 | <HR> |
---|
25 | <H3>Contents</H3> |
---|
26 | <dl class="index"> |
---|
27 | <dt><A href="#synopsis">Synopsis</A> <dt><a href="#description">Description</a> <dt><A href="#examples"> |
---|
28 | Examples</A></dt></dl> |
---|
29 | <H3><A name="synopsis"></A>Synopsis</H3> |
---|
30 | <PRE>#include <<A href="../../../boost/regex.hpp">boost/regex.hpp</A>> </PRE> |
---|
31 | <P>The algorithm regex_replace searches through a string finding |
---|
32 | all the matches to the regular expression: for each match it then calls <A href="match_results.html#format"> |
---|
33 | match_results::format</A> to format the string and sends the result to the |
---|
34 | output iterator. Sections of text that do not match are copied to the output |
---|
35 | unchanged only if the <EM>flags</EM> parameter does not have the flag <A href="match_flag_type.html"> |
---|
36 | format_no_copy</A> set. If the flag <A href="match_flag_type.html">format_first_only</A> |
---|
37 | is set then only the first occurrence is replaced rather than all |
---|
38 | occurrences. <PRE>template <class OutputIterator, class BidirectionalIterator, class traits, class charT> |
---|
39 | OutputIterator <A href="#f1">regex_replace</A>(OutputIterator out, |
---|
40 | BidirectionalIterator first, |
---|
41 | BidirectionalIterator last, |
---|
42 | const <A href="basic_regex.html">basic_regex</A><charT, traits>& e, |
---|
43 | const basic_string<charT>& fmt, |
---|
44 | <A href="match_flag_type.html">match_flag_type flags = match_default</A>); |
---|
45 | |
---|
46 | template <class traits, class charT> |
---|
47 | basic_string<charT> <A href="#f2">regex_replace</A>(const basic_string<charT>& s, |
---|
48 | const <A href="basic_regex.html">basic_regex</A><charT, traits>& e, |
---|
49 | const basic_string<charT>& fmt, |
---|
50 | <A href="match_flag_type.html">match_flag_type flags = match_default</A>); |
---|
51 | |
---|
52 | </PRE> |
---|
53 | <H3><A name="description"></A>Description</H3> |
---|
54 | <PRE><A name=f1></A>template <class OutputIterator, class BidirectionalIterator, class traits, class charT> |
---|
55 | OutputIterator regex_replace(OutputIterator out, |
---|
56 | BidirectionalIterator first, |
---|
57 | BidirectionalIterator last, |
---|
58 | const <A href="basic_regex.html">basic_regex</A><charT, traits>& e, |
---|
59 | const basic_string<charT>& fmt, |
---|
60 | <A href="match_flag_type.html">match_flag_type flags = match_default</A>);</PRE> |
---|
61 | <P>Enumerates all the occurences of expression <EM>e</EM> in the sequence [first, |
---|
62 | last), replacing each occurence with the string that results by merging the |
---|
63 | match found with the format string <EM>fmt</EM>, and copies the resulting |
---|
64 | string to <EM>out</EM>. </P> |
---|
65 | <P>If the flag format_no_copy is set in <EM>flags</EM> then unmatched sections of |
---|
66 | text are not copied to output. |
---|
67 | </P> |
---|
68 | <P>If the flag format_first_only is set in <EM>flags</EM> then only the first |
---|
69 | occurence of <EM>e</EM> is replaced. |
---|
70 | </P> |
---|
71 | <P>The manner in which the format string <EM>fmt</EM> is interpretted, along with |
---|
72 | the rules used for finding matches, are determined by the <A href="match_flag_type.html"> |
---|
73 | flags</A> set in <EM>flags</EM></P> |
---|
74 | <P><B>Effects:</B> Constructs an |
---|
75 | <SPAN class="spelle">regex_iterator</SPAN> |
---|
76 | object: |
---|
77 | </P> |
---|
78 | <PRE><SPAN style="FONT-SIZE: 10pt">regex_iterator<</SPAN><SPAN class=spelle><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">BidirectionalIterator</SPAN></SPAN><SPAN style="FONT-SIZE: 10pt">, </SPAN><SPAN class=spelle><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">charT</SPAN></SPAN><SPAN style="FONT-SIZE: 10pt">, traits, Allocator> <BR> </SPAN><SPAN class=grame><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">i(</SPAN></SPAN><SPAN style="FONT-SIZE: 10pt">first, last, e, flags)</SPAN>, </PRE> |
---|
79 | <P>and uses |
---|
80 | <SPAN class="spelle"> |
---|
81 | <I>i</I></SPAN> |
---|
82 | to enumerate through all of the matches <I>m</I> of type |
---|
83 | <SPAN class="spelle"> |
---|
84 | <SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">match_results</SPAN> |
---|
85 | </SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><<SPAN class="spelle">BidirectionalIterator</SPAN>> </SPAN>that |
---|
86 | occur within the sequence [first, last). |
---|
87 | </P> |
---|
88 | <P>If no such matches are found |
---|
89 | <SPAN class="grame">and </SPAN></P> |
---|
90 | <PRE><SPAN class=grame></SPAN><SPAN class=grame><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">!</SPAN></SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">(flags & <SPAN class=spelle>format_no_copy</SPAN>)</SPAN> </PRE> |
---|
91 | <P>then calls |
---|
92 | </P> |
---|
93 | <PRE><SPAN class=spelle><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">std::copy</SPAN></SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">(first, last, out)</SPAN>. </PRE> |
---|
94 | <P>Otherwise, for each match found, |
---|
95 | <SPAN class="grame">if </SPAN></P> |
---|
96 | <PRE><SPAN class=grame></SPAN><SPAN class=grame><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">!</SPAN></SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">(flags & <SPAN class=spelle>format_no_copy</SPAN>)</SPAN> </PRE> |
---|
97 | <P>calls |
---|
98 | </P> |
---|
99 | <PRE><SPAN class=spelle><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">std::copy</SPAN></SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">(<SPAN class=spelle>m.prefix</SPAN>().first, <SPAN class=spelle>m.prefix</SPAN>().last, out)</SPAN>, </PRE> |
---|
100 | <P>and then calls |
---|
101 | </P> |
---|
102 | <PRE><SPAN class=spelle><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">m.format</SPAN></SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">(out, <SPAN class=spelle>fmt</SPAN>, flags)</SPAN>. </PRE> |
---|
103 | <P>Finally |
---|
104 | <SPAN class="grame">if </SPAN></P> |
---|
105 | <PRE><SPAN class=grame></SPAN><SPAN class=grame><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">!</SPAN></SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">(flags & <SPAN class=spelle>format_no_copy</SPAN>)</SPAN> </PRE> |
---|
106 | <P>calls |
---|
107 | </P> |
---|
108 | <PRE><SPAN class=spelle><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">std::copy</SPAN></SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">(<SPAN class=spelle>last_m.suffix</SPAN>().first, <SPAN class=spelle>last_m,suffix</SPAN>().last, out) </SPAN></PRE> |
---|
109 | <P><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"></SPAN>where |
---|
110 | <SPAN class="spelle"> |
---|
111 | <SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">last_m</SPAN> |
---|
112 | </SPAN> |
---|
113 | is a copy of the last match found. |
---|
114 | </P> |
---|
115 | <P>If |
---|
116 | <SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">flags & |
---|
117 | <SPAN class="spelle">format_first_only</SPAN></SPAN> |
---|
118 | is non-zero then only the first match found is replaced.</P> |
---|
119 | <P><STRONG>Throws:</STRONG> <CODE>std::runtime_error</CODE> if the complexity of |
---|
120 | matching the expression against an N character string begins to exceed O(N<SUP>2</SUP>), |
---|
121 | or if the program runs out of stack space while matching the expression (if |
---|
122 | Boost.regex is <A href="configuration.html">configured</A> in recursive mode), |
---|
123 | or if the matcher exhausts it's permitted memory allocation (if Boost.regex is <A href="configuration.html"> |
---|
124 | configured</A> in non-recursive mode).</P> |
---|
125 | <P><B> Returns:</B> <CODE>out</CODE>. |
---|
126 | </P> |
---|
127 | <PRE><A name=f2></A>template <class traits, class charT> |
---|
128 | basic_string<charT> regex_replace(const basic_string<charT>& s, |
---|
129 | const <A href="basic_regex.html">basic_regex</A><charT, traits>& e, |
---|
130 | const basic_string<charT>& fmt, |
---|
131 | <A href="match_flag_type.html">match_flag_type flags = match_default</A>);</PRE> |
---|
132 | <P><B> Effects:</B> Constructs an object <CODE>basic_string<charT> result</CODE>, |
---|
133 | calls <CODE>regex_replace(back_inserter(result), s.begin(), s.end(), e, fmt, |
---|
134 | flags)</CODE>, and then returns <CODE>result</CODE>. |
---|
135 | <H3><A name="examples"></A>Examples</H3> |
---|
136 | <P>The following <A href="../example/snippets/regex_replace_example.cpp">example</A> |
---|
137 | takes C/C++ source code as input, and outputs syntax highlighted HTML code.</P> |
---|
138 | <P></P> |
---|
139 | <PRE><FONT color=#008080>#include <fstream> |
---|
140 | #include <sstream> |
---|
141 | #include <string> |
---|
142 | #include <iterator> |
---|
143 | #include <boost/regex.hpp> |
---|
144 | #include <fstream> |
---|
145 | #include <iostream> |
---|
146 | </FONT> |
---|
147 | <FONT color=#000080><I>// purpose: |
---|
148 | // takes the contents of a file and transform to |
---|
149 | // syntax highlighted code in html format |
---|
150 | </I></FONT> |
---|
151 | boost::regex e1, e2; |
---|
152 | <B>extern</B> <B>const</B> <B>char</B>* expression_text; |
---|
153 | <B>extern</B> <B>const</B> <B>char</B>* format_string; |
---|
154 | <B>extern</B> <B>const</B> <B>char</B>* pre_expression; |
---|
155 | <B>extern</B> <B>const</B> <B>char</B>* pre_format; |
---|
156 | <B>extern</B> <B>const</B> <B>char</B>* header_text; |
---|
157 | <B>extern</B> <B>const</B> <B>char</B>* footer_text; |
---|
158 | |
---|
159 | <B>void</B> load_file(std::string& s, std::istream& is) |
---|
160 | { |
---|
161 | s.erase(); |
---|
162 | s.reserve(is.rdbuf()->in_avail()); |
---|
163 | <B>char</B> c; |
---|
164 | <B>while</B>(is.get(c)) |
---|
165 | { |
---|
166 | <B>if</B>(s.capacity() == s.size()) |
---|
167 | s.reserve(s.capacity() * <FONT color=#000080>3</FONT>); |
---|
168 | s.append(<FONT color=#000080>1</FONT>, c); |
---|
169 | } |
---|
170 | } |
---|
171 | |
---|
172 | <B>int</B> main(<B>int</B> argc, <B>const</B> <B>char</B>** argv) |
---|
173 | { |
---|
174 | try{ |
---|
175 | e1.assign(expression_text); |
---|
176 | e2.assign(pre_expression); |
---|
177 | <B>for</B>(<B>int</B> i = <FONT color=#000080>1</FONT>; i < argc; ++i) |
---|
178 | { |
---|
179 | std::cout << <FONT color=#0000ff>"Processing file "</FONT> << argv[i] << std::endl; |
---|
180 | std::ifstream fs(argv[i]); |
---|
181 | std::string in; |
---|
182 | load_file(in, fs); |
---|
183 | std::string out_name(std::string(argv[i]) + std::string(<FONT color=#0000ff>".htm"</FONT>)); |
---|
184 | std::ofstream os(out_name.c_str()); |
---|
185 | os << header_text; |
---|
186 | <FONT color=#000080><I>// strip '<' and '>' first by outputting to a |
---|
187 | </I></FONT> <FONT color=#000080><I>// temporary string stream |
---|
188 | </I></FONT> std::ostringstream t(std::ios::out | std::ios::binary); |
---|
189 | std::ostream_iterator<<B>char</B>, <B>char</B>> oi(t); |
---|
190 | boost::regex_replace(oi, in.begin(), in.end(), |
---|
191 | e2, pre_format, boost::match_default | boost::format_all); |
---|
192 | <FONT color=#000080><I>// then output to final output stream |
---|
193 | </I></FONT> <FONT color=#000080><I>// adding syntax highlighting: |
---|
194 | </I></FONT> std::string s(t.str()); |
---|
195 | std::ostream_iterator<<B>char</B>, <B>char</B>> out(os); |
---|
196 | boost::regex_replace(out, s.begin(), s.end(), |
---|
197 | e1, format_string, boost::match_default | boost::format_all); |
---|
198 | os << footer_text; |
---|
199 | } |
---|
200 | } |
---|
201 | <STRONG>catch</STRONG>(...) |
---|
202 | { <STRONG>return</STRONG> -1; } |
---|
203 | <B>return</B> <FONT color=#000080>0</FONT>; |
---|
204 | } |
---|
205 | |
---|
206 | <B>extern</B> <B>const</B> <B>char</B>* pre_expression = <FONT color=#0000ff>"(<)|(>)|\\r"</FONT>; |
---|
207 | <B>extern</B> <B>const</B> <B>char</B>* pre_format = <FONT color=#0000ff>"(?1<)(?2>)"</FONT>; |
---|
208 | |
---|
209 | |
---|
210 | <B>const</B> <B>char</B>* expression_text = <FONT color=#000080><I>// preprocessor directives: index 1 |
---|
211 | </I></FONT> <FONT color=#0000ff>"(^[[:blank:]]*#(?:[^\\\\\\n]|\\\\[^\\n[:punct:][:word:]]*[\\n[:punct:][:word:]])*)|" |
---|
212 | </FONT> <FONT color=#000080><I>// comment: index 2 |
---|
213 | </I></FONT> <FONT color=#0000ff>"(//[^\\n]*|/\\*.*?\\*/)|" |
---|
214 | </FONT> <FONT color=#000080><I>// literals: index 3 |
---|
215 | </I></FONT> <FONT color=#0000ff>"\\<([+-]?(?:(?:0x[[:xdigit:]]+)|(?:(?:[[:digit:]]*\\.)?[[:digit:]]+(?:[eE][+-]?[[:digit:]]+)?))u?(?:(?:int(?:8|16|32|64))|L)?)\\>|" |
---|
216 | </FONT> <FONT color=#000080><I>// string literals: index 4 |
---|
217 | </I></FONT> <FONT color=#0000ff>"('(?:[^\\\\']|\\\\.)*'|\"(?:[^\\\\\"]|\\\\.)*\")|" |
---|
218 | </FONT> <FONT color=#000080><I>// keywords: index 5 |
---|
219 | </I></FONT> <FONT color=#0000ff>"\\<(__asm|__cdecl|__declspec|__export|__far16|__fastcall|__fortran|__import" |
---|
220 | </FONT> <FONT color=#0000ff>"|__pascal|__rtti|__stdcall|_asm|_cdecl|__except|_export|_far16|_fastcall" |
---|
221 | </FONT> <FONT color=#0000ff>"|__finally|_fortran|_import|_pascal|_stdcall|__thread|__try|asm|auto|bool" |
---|
222 | </FONT> <FONT color=#0000ff>"|break|case|catch|cdecl|char|class|const|const_cast|continue|default|delete" |
---|
223 | </FONT> <FONT color=#0000ff>"|do|double|dynamic_cast|else|enum|explicit|extern|false|float|for|friend|goto" |
---|
224 | </FONT> <FONT color=#0000ff>"|if|inline|int|long|mutable|namespace|new|operator|pascal|private|protected" |
---|
225 | </FONT> <FONT color=#0000ff>"|public|register|reinterpret_cast|return|short|signed|sizeof|static|static_cast" |
---|
226 | </FONT> <FONT color=#0000ff>"|struct|switch|template|this|throw|true|try|typedef|typeid|typename|union|unsigned" |
---|
227 | </FONT> <FONT color=#0000ff>"|using|virtual|void|volatile|wchar_t|while)\\>" |
---|
228 | </FONT> ; |
---|
229 | |
---|
230 | <B>const</B> <B>char</B>* format_string = <FONT color=#0000ff>"(?1<font color=\"#008040\">$&</font>)" |
---|
231 | </FONT> <FONT color=#0000ff>"(?2<I><font color=\"#000080\">$&</font></I>)" |
---|
232 | </FONT> <FONT color=#0000ff>"(?3<font color=\"#0000A0\">$&</font>)" |
---|
233 | </FONT> <FONT color=#0000ff>"(?4<font color=\"#0000FF\">$&</font>)" |
---|
234 | </FONT> <FONT color=#0000ff>"(?5<B>$&</B>)"</FONT>; |
---|
235 | |
---|
236 | <B>const</B> <B>char</B>* header_text = <FONT color=#0000ff>"<HTML>\n<HEAD>\n" |
---|
237 | </FONT> <FONT color=#0000ff>"<TITLE>Auto-generated html formated source</TITLE>\n" |
---|
238 | </FONT> <FONT color=#0000ff>"<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=windows-1252\">\n" |
---|
239 | </FONT> <FONT color=#0000ff>"</HEAD>\n" |
---|
240 | </FONT> <FONT color=#0000ff>"<BODY LINK=\"#0000ff\" VLINK=\"#800080\" BGCOLOR=\"#ffffff\">\n" |
---|
241 | </FONT> <FONT color=#0000ff>"<P> </P>\n<PRE>"</FONT>; |
---|
242 | |
---|
243 | <B>const</B> <B>char</B>* footer_text = <FONT color=#0000ff>"</PRE>\n</BODY>\n\n"</FONT>; |
---|
244 | </PRE> |
---|
245 | <HR> |
---|
246 | <p>Revised |
---|
247 | <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan --> |
---|
248 | 26 June 2004 |
---|
249 | <!--webbot bot="Timestamp" endspan i-checksum="39359" --></p> |
---|
250 | <p><i>© Copyright John Maddock 1998- |
---|
251 | <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%Y" startspan --> 2004<!--webbot bot="Timestamp" endspan i-checksum="39359" --></i></p> |
---|
252 | <P><I>Use, modification and distribution are subject to the Boost Software License, |
---|
253 | Version 1.0. (See accompanying file <A href="../../../LICENSE_1_0.txt">LICENSE_1_0.txt</A> |
---|
254 | or copy at <A href="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</A>)</I></P> |
---|
255 | </body> |
---|
256 | </html> |
---|