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