Files
regex/doc/format_syntax.html

273 lines
7.4 KiB
HTML
Raw Normal View History

2003-05-17 11:45:48 +00:00
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta name="generator" content="HTML Tidy, see www.w3.org">
<title>Boost.Regex: Format String Syntax</title>
<meta http-equiv="Content-Type" content=
"text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="../../../boost.css">
</head>
<body>
<p></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" width="277" alt=
"C++ Boost" src="../../../c++boost.gif" border="0"></a></h3>
</td>
<td width="353">
<h1 align="center">Boost.Regex</h1>
<h2 align="center">Format String Syntax</h2>
</td>
<td width="50">
<h3><a href="index.html"><img height="45" width="43" alt=
"Boost.Regex Index" src="uarrow.gif" border="0"></a></h3>
</td>
</tr>
</table>
<br>
<br>
<hr>
<p>Format strings are used by the algorithm <a href="regex_replace.html">regex_replace</a>&nbsp;and by <a
2003-05-17 11:45:48 +00:00
href="match_results.html">match_results::format</a>, and are used
to transform one string into another.</p>
<p>There are three kind of format string: sed, Perl and extended,
the extended syntax is a superset of the others so this is covered
first.</p>
<p><b><i>Extended format syntax</i></b></p>
<p>In format strings, all characters are treated as literals
except: ()$\?:</p>
<p>To use any of these as literals you must prefix them with the
escape character \</p>
<p>The following special sequences are recognized:&nbsp;<br>
&nbsp;<br>
<i>Grouping:</i></p>
<p>Use the parenthesis characters ( and ) to group sub-expressions
within the format string, use \( and \) to represent literal '('
and ')'.&nbsp;<br>
&nbsp;<br>
<i>Sub-expression expansions:</i></p>
<p>The following Perl like expressions expand to a particular
matched sub-expression:<br>
&nbsp;</p>
<p></p>
<table id="Table2" cellspacing="0" cellpadding="7" width="100%"
border="0">
<tr>
<td valign="top" width="8%">&nbsp;</td>
<td valign="top" width="40%">$`</td>
<td valign="top" width="43%">Expands to all the text from the end
of the previous match to the start of the current match, if there
was no previous match in the current operation, then everything
from the start of the input string to the start of the match.</td>
<td valign="top" width="9%">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="8%">&nbsp;</td>
<td valign="top" width="40%">$'</td>
<td valign="top" width="43%">Expands to all the text from the end
of the match to the end of the input string.</td>
<td valign="top" width="9%">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="8%">&nbsp;</td>
<td valign="top" width="40%">$&amp;</td>
<td valign="top" width="43%">Expands to all of the current
match.</td>
<td valign="top" width="9%">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="8%">&nbsp;</td>
<td valign="top" width="40%">$0</td>
<td valign="top" width="43%">Expands to all of the current
match.</td>
<td valign="top" width="9%">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="8%">&nbsp;</td>
<td valign="top" width="40%">$N</td>
<td valign="top" width="43%">Expands to the text that matched
sub-expression <i>N</i>.</td>
<td valign="top" width="9%">&nbsp;</td>
</tr>
</table>
<br>
<br>
<p><i>Conditional expressions:</i></p>
<p>Conditional expressions allow two different format strings to be
selected dependent upon whether a sub-expression participated in
the match or not:</p>
<p>?Ntrue_expression:false_expression</p>
<p>Executes true_expression if sub-expression <i>N</i> participated
in the match, otherwise executes false_expression.</p>
<p>Example: suppose we search for "(while)|(for)" then the format
string "?1WHILE:FOR" would output what matched, but in upper
case.&nbsp;<br>
&nbsp;<br>
<i>Escape sequences:</i></p>
<p>The following escape sequences are also allowed:<br>
</p>
<p></p>
<table id="Table3" cellspacing="0" cellpadding="7" width="100%"
border="0">
<tr>
<td valign="top" width="8%">&nbsp;</td>
<td valign="top" width="40%">\a</td>
<td valign="top" width="43%">The bell character.</td>
<td valign="top" width="9%">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="8%">&nbsp;</td>
<td valign="top" width="40%">\f</td>
<td valign="top" width="43%">The form feed character.</td>
<td valign="top" width="9%">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="8%">&nbsp;</td>
<td valign="top" width="40%">\n</td>
<td valign="top" width="43%">The newline character.</td>
<td valign="top" width="9%">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="8%">&nbsp;</td>
<td valign="top" width="40%">\r</td>
<td valign="top" width="43%">The carriage return character.</td>
<td valign="top" width="9%">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="8%">&nbsp;</td>
<td valign="top" width="40%">\t</td>
<td valign="top" width="43%">The tab character.</td>
<td valign="top" width="9%">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="8%">&nbsp;</td>
<td valign="top" width="40%">\v</td>
<td valign="top" width="43%">A vertical tab character.</td>
<td valign="top" width="9%">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="8%">&nbsp;</td>
<td valign="top" width="40%">\x</td>
<td valign="top" width="43%">A hexadecimal character - for example
\x0D.</td>
<td valign="top" width="9%">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="8%">&nbsp;</td>
<td valign="top" width="40%">\x{}</td>
<td valign="top" width="43%">A possible Unicode hexadecimal
character - for example \x{1A0}</td>
<td valign="top" width="9%">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="8%">&nbsp;</td>
<td valign="top" width="40%">\cx</td>
<td valign="top" width="43%">The ASCII escape character x, for
example \c@ is equivalent to escape-@.</td>
<td valign="top" width="9%">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="8%">&nbsp;</td>
<td valign="top" width="40%">\e</td>
<td valign="top" width="43%">The ASCII escape character.</td>
<td valign="top" width="9%">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="8%">&nbsp;</td>
<td valign="top" width="40%">\dd</td>
<td valign="top" width="43%">An octal character constant, for
example \10.</td>
<td valign="top" width="9%">&nbsp;</td>
</tr>
</table>
<br>
<br>
<p><b><i>Perl format strings</i></b></p>
<p>Perl format strings are the same as the default syntax except
that the characters ()?: have no special meaning.</p>
<p><b><i>Sed format strings</i></b></p>
<p>Sed format strings use only the characters \ and &amp; as
special characters.</p>
<p>\n where n is a digit, is expanded to the nth
sub-expression.</p>
<p>&amp; is expanded to the whole of the match (equivalent to
\0).</p>
<p>Other escape sequences are expanded as per the default
syntax.</p>
<p></p>
<hr>
<p>Revised
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->
17 May 2003
<!--webbot bot="Timestamp" endspan i-checksum="39359" --></p>
<p><i>&copy; Copyright <a href="mailto:jm@regex.fsnet.co.uk">John
Maddock</a>&nbsp;1998-
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%Y" startspan -->
2003
<!--webbot bot="Timestamp" endspan i-checksum="39359" --></i></p>
<p align="left"><i>Permission to use, copy, modify, distribute and
sell this software and its documentation for any purpose is hereby
granted without fee, provided that the above copyright notice
appear in all copies and that both that copyright notice and this
permission notice appear in supporting documentation. Dr John
Maddock makes no representations about the suitability of this
software for any purpose. It is provided "as is" without express or
implied warranty.</i></p>
</body>
</html>