Files
hustoj/web/cppreference-zh/chmhelp/cpp-regex-match_results-format.html
2025-08-01 12:57:54 +08:00

174 lines
14 KiB
HTML

<!DOCTYPE html>
<html lang="zh" dir="ltr" class="client-nojs" xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh">
<head><meta http-equiv="x-ua-compatible" content="ie=edge">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>std::match_results&lt;BidirIt,Alloc&gt;::format</title>
<link rel="stylesheet" href="ext.css" />
<link rel="stylesheet" href="site_modules.css" />
</head>
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-cpp_regex_match_results_format skin-cppreference2 action-view cpp-navbar">
<div id="cpp-content-base">
<div id="content"><a id="top"></a>
<h1 id="firstHeading" class="firstHeading"><span style="font-size:0.7em; line-height:130%">std::match_results&lt;BidirIt,Alloc&gt;::</span>format</h1>
<div id="bodyContent">
<div id="contentSub"><span class="subpages">&lt; <a href="cpp.html">cpp</a>&lrm; | <a href="cpp-regex.html">regex</a>&lrm; | <a href="cpp-regex-match_results.html">match results</a></span></div>
<div id="mw-content-text" lang="zh" dir="ltr" class="mw-content-ltr" xml:lang="zh">
<table class="t-dcl-begin">
<tbody>
<tr class="t-dcl t-since-cxx11">
<td>
<div><span class="mw-geshi cpp source-cpp"><span class="kw1">template</span><span class="sy1">&lt;</span> <span class="kw1">class</span> OutputIt <span class="sy1">&gt;</span><br /></span>
<p>OutputIt format<span class="br0">(</span> OutputIt out,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw4">const</span> char_type<span class="sy2">*</span> fmt_first, <span class="kw4">const</span> char_type<span class="sy2">*</span> fmt_last,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="cpp-regex-match_flag_type.html"><span class="kw2038">std::<span class="me2">regex_constants</span><span class="sy4">::</span><span class="me2">match_flag_type</span></span></a> flags <span class="sy1">=</span><br /></p>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="cpp-regex-match_flag_type.html"><span class="kw2048">std::<span class="me2">regex_constants</span><span class="sy4">::</span><span class="me2">format_default</span></span></a> <span class="br0">)</span> <span class="kw4">const</span><span class="sy4">;</span></div>
</td>
<td>(1)</td>
<td><span class="t-mark-rev t-since-cxx11">(C++11 &#36215;)</span></td>
</tr>
<tr class="t-dcl t-since-cxx11">
<td>
<div><span class="mw-geshi cpp source-cpp"><span class="kw1">template</span><span class="sy1">&lt;</span> <span class="kw1">class</span> OutputIt, <span class="kw1">class</span> ST, <span class="kw1">class</span> SA <span class="sy1">&gt;</span><br /></span>
<p>OutputIt format<span class="br0">(</span> OutputIt out,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw4">const</span> basic_string<span class="sy1">&lt;</span>char_type,ST,SA<span class="sy1">&gt;</span><span class="sy3">&amp;</span> fmt,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="cpp-regex-match_flag_type.html"><span class="kw2038">std::<span class="me2">regex_constants</span><span class="sy4">::</span><span class="me2">match_flag_type</span></span></a> flags <span class="sy1">=</span><br /></p>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="cpp-regex-match_flag_type.html"><span class="kw2048">std::<span class="me2">regex_constants</span><span class="sy4">::</span><span class="me2">format_default</span></span></a> <span class="br0">)</span> <span class="kw4">const</span><span class="sy4">;</span></div>
</td>
<td>(2)</td>
<td><span class="t-mark-rev t-since-cxx11">(C++11 &#36215;)</span></td>
</tr>
<tr class="t-dcl t-since-cxx11">
<td>
<div><span class="mw-geshi cpp source-cpp"><span class="kw1">template</span><span class="sy1">&lt;</span> <span class="kw1">class</span> ST, <span class="kw1">class</span> SA <span class="sy1">&gt;</span><br /></span>
<p><a href="cpp-string-basic_string.html"><span class="kw1228">std::<span class="me2">basic_string</span></span></a><span class="sy1">&lt;</span>char_type,ST,SA<span class="sy1">&gt;</span><br />
&nbsp; &nbsp; format<span class="br0">(</span> <span class="kw4">const</span> <a href="cpp-string-basic_string.html"><span class="kw1228">std::<span class="me2">basic_string</span></span></a><span class="sy1">&lt;</span>char_type,ST,SA<span class="sy1">&gt;</span><span class="sy3">&amp;</span> fmt,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="cpp-regex-match_flag_type.html"><span class="kw2038">std::<span class="me2">regex_constants</span><span class="sy4">::</span><span class="me2">match_flag_type</span></span></a> flags <span class="sy1">=</span><br /></p>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="cpp-regex-match_flag_type.html"><span class="kw2048">std::<span class="me2">regex_constants</span><span class="sy4">::</span><span class="me2">format_default</span></span></a> <span class="br0">)</span> <span class="kw4">const</span><span class="sy4">;</span></div>
</td>
<td>(3)</td>
<td><span class="t-mark-rev t-since-cxx11">(C++11 &#36215;)</span></td>
</tr>
<tr class="t-dcl t-since-cxx11">
<td>
<div><span class="mw-geshi cpp source-cpp">string_type format<span class="br0">(</span> <span class="kw4">const</span> char_type<span class="sy2">*</span> fmt_s,<br /></span>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="cpp-regex-match_flag_type.html"><span class="kw2038">std::<span class="me2">regex_constants</span><span class="sy4">::</span><span class="me2">match_flag_type</span></span></a> flags <span class="sy1">=</span><br /></p>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="cpp-regex-match_flag_type.html"><span class="kw2048">std::<span class="me2">regex_constants</span><span class="sy4">::</span><span class="me2">format_default</span></span></a> <span class="br0">)</span> <span class="kw4">const</span><span class="sy4">;</span></div>
</td>
<td>(4)</td>
<td><span class="t-mark-rev t-since-cxx11">(C++11 &#36215;)</span></td>
</tr>
<tr class="t-dcl-sep">
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<p><code>format</code> &#36755;&#20986;&#26684;&#24335;&#23383;&#31526;&#20018;&#65292;&#23427;&#23558;&#23383;&#31526;&#20018;&#20013;&#30340;&#20219;&#20309;&#26684;&#24335;&#25351;&#23450;&#31526;&#25110;&#36716;&#20041;&#24207;&#21015;&#26367;&#25442;&#20026;&#26469;&#33258; <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="sy2">*</span>this</span></span> &#30340;&#25968;&#25454;&#12290;</p>
<div class="t-li1"><span class="t-li">1)</span> &#26684;&#24335;&#23383;&#31526;&#24207;&#21015;&#20197;&#33539;&#22260; <code>[fmt_first, fmt_last)</code> &#23450;&#20041;&#12290;&#22797;&#21046;&#32467;&#26524;&#23383;&#31526;&#24207;&#21015;&#21040; <code>out</code> &#30340;&#21103;&#26412;&#12290;</div>
<div class="t-li1"><span class="t-li">2)</span> &#26684;&#24335;&#23383;&#31526;&#24207;&#21015;&#20197; <code>fmt</code> &#20013;&#30340;&#23383;&#31526;&#23450;&#20041;&#12290;&#22797;&#21046;&#32467;&#26524;&#23383;&#31526;&#24207;&#21015;&#21040; <code>out</code> &#30340;&#21103;&#26412;&#12290;</div>
<div class="t-li1"><span class="t-li">3-4)</span> &#26684;&#24335;&#23383;&#31526;&#24207;&#21015;&#20998;&#21035;&#20197; <code>fmt</code> &#21644; <code>fmt_s</code> &#20013;&#30340;&#23383;&#31526;&#23450;&#20041;&#12290;&#22797;&#21046;&#32467;&#26524;&#23383;&#31526;&#24207;&#21015;&#21040;&#26032;&#26500;&#36896;&#30340; <span class="t-lc"><a href="cpp-string-basic_string.html">std::basic_string</a></span> &#65292;&#24182;&#36820;&#22238;&#23427;&#12290;</div>
<p><code>flags</code> &#20301;&#25513;&#30721;&#30830;&#23450;&#36776;&#35782;&#21738;&#20123;&#26684;&#24335;&#25351;&#23450;&#31526;&#21644;&#36716;&#20041;&#24207;&#21015;&#12290;</p>
<p>&#33509; <span class="t-c"><span class="mw-geshi cpp source-cpp">ready<span class="br0">(</span><span class="br0">)</span> <span class="sy3">!</span><span class="sy1">=</span> <span class="kw2">true</span></span></span> &#21017; <code>format</code> &#30340;&#34892;&#20026;&#26410;&#23450;&#20041;&#12290;</p>
<h3><span class="mw-headline" id=".E5.8F.82.E6.95.B0">&#21442;&#25968;</span></h3>
<table class="t-par-begin">
<tr class="t-par">
<td>fmt_begin, fmt_end</td>
<td>-</td>
<td>&#25351;&#21521;&#23450;&#20041;&#26684;&#24335;&#23383;&#31526;&#24207;&#21015;&#30340;&#23383;&#31526;&#33539;&#22260;&#30340;&#25351;&#38024;</td>
</tr>
<tr class="t-par">
<td>fmt</td>
<td>-</td>
<td>&#23450;&#20041;&#26684;&#24335;&#23383;&#31526;&#24207;&#21015;&#30340; <span class="t-lc"><a href="cpp-string-basic_string.html">std::basic_string</a></span></td>
</tr>
<tr class="t-par">
<td>fmt_s</td>
<td>-</td>
<td>&#25351;&#21521;&#23450;&#20041;&#26684;&#24335;&#23383;&#31526;&#24207;&#21015;&#30340;&#31354;&#32456;&#27490;&#23383;&#31526;&#20018;&#30340;&#25351;&#38024;</td>
</tr>
<tr class="t-par">
<td>out</td>
<td>-</td>
<td>&#35201;&#22797;&#21046;&#32467;&#26524;&#23383;&#31526;&#20018;&#21040;&#30340;&#36845;&#20195;&#22120;</td>
</tr>
<tr class="t-par">
<td>flags</td>
<td>-</td>
<td>&#25351;&#23450;&#36776;&#35782;&#21738;&#20123;&#26684;&#24335;&#25351;&#23450;&#31526;&#21644;&#36716;&#20041;&#24207;&#21015;&#30340; <span class="t-lc"><a href="cpp-regex-match_flag_type.html">std::regex_constants::match_flag_type</a></span> &#20301;&#25513;&#30721;</td>
</tr>
<tr class="t-par-hitem">
<td colspan="3">&#31867;&#22411;&#35201;&#27714;</td>
</tr>
<tr class="t-par-req">
<td colspan="3">-
<div class="spacer"></div>
<code>OutputIt</code> &#24517;&#39035;&#28385;&#36275;<a href="cpp-named_req-OutputIterator.html"><span style="font-style:italic">&#32769;&#24335;&#36755;&#20986;&#36845;&#20195;&#22120;</span> <span style="font-family: Georgia, &#39;DejaVu Serif&#39;, serif; font-style:italic">(LegacyOutputIterator)</span></a> &#30340;&#35201;&#27714;&#12290;</td>
</tr>
</table>
<h3><span class="mw-headline" id=".E8.BF.94.E5.9B.9E.E5.80.BC">&#36820;&#22238;&#20540;</span></h3>
<div class="t-li1"><span class="t-li">1-2)</span> <code>out</code></div>
<div class="t-li1"><span class="t-li">3-4)</span> &#21547;&#32467;&#26524;&#23383;&#31526;&#24207;&#21015;&#30340;&#26032;&#26500;&#36896;&#23383;&#31526;&#20018;&#12290;</div>
<h3><span class="mw-headline" id=".E5.BC.82.E5.B8.B8">&#24322;&#24120;</span></h3>
<p>&#21487;&#33021;&#25243;&#20986;&#23454;&#29616;&#23450;&#20041;&#30340;&#24322;&#24120;&#12290;</p>
<h3><span class="mw-headline" id=".E7.A4.BA.E4.BE.8B">&#31034;&#20363;</span></h3>
<div class="t-example">
<div class="t-example-live-link"></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;">
<div class="cpp source-cpp">
<pre class="de1"><span class="co2">#include &lt;iostream&gt;</span>
<span class="co2">#include &lt;string&gt;</span>
<span class="co2">#include &lt;regex&gt;</span>
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
<a href="cpp-string-basic_string.html"><span class="kw1230">std::<span class="me2">string</span></span></a> s <span class="sy1">=</span> <span class="st0">&quot;for a good time, call 867-5309&quot;</span><span class="sy4">;</span>
<a href="cpp-regex-basic_regex.html"><span class="kw2000">std::<span class="me2">regex</span></span></a> phone_regex<span class="br0">(</span><span class="st0">&quot;<span class="es1">\\</span>d{3}-<span class="es1">\\</span>d{4}&quot;</span><span class="br0">)</span><span class="sy4">;</span>
<a href="cpp-regex-match_results.html"><span class="kw2010">std::<span class="me2">smatch</span></span></a> phone_match<span class="sy4">;</span>
<span class="kw1">if</span> <span class="br0">(</span><a href="cpp-regex-regex_search.html"><span class="kw2013">std::<span class="me2">regex_search</span></span></a><span class="br0">(</span>s, phone_match, phone_regex<span class="br0">)</span><span class="br0">)</span> <span class="br0">{</span>
<a href="cpp-string-basic_string.html"><span class="kw1230">std::<span class="me2">string</span></span></a> fmt_s <span class="sy1">=</span> phone_match.<span class="me1">format</span><span class="br0">(</span>
<span class="st0">&quot;$`&quot;</span> <span class="co1">// $` &#24847;&#21619;&#30528;&#21305;&#37197;&#20043;&#21069;&#30340;&#23383;&#31526;</span>
<span class="st0">&quot;[$&amp;]&quot;</span> <span class="co1">// $&amp; &#24847;&#21619;&#30528;&#21305;&#37197;&#30340;&#23383;&#31526;</span>
<span class="st0">&quot;$&#39;&quot;</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// $&#39; &#24847;&#21619;&#30528;&#21518;&#38543;&#21305;&#37197;&#30340;&#23383;&#31526;</span>
<a href="cpp-io-cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> fmt_s <span class="sy1">&lt;&lt;</span> <span class="st0">&#39;<span class="es1">\n</span>&#39;</span><span class="sy4">;</span>
<span class="br0">}</span>
<span class="br0">}</span></pre></div>
</div>
<p>&#36755;&#20986;&#65306;</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;">
<div class="text source-text">
<pre class="de1">for a good time, call [867-5309]</pre></div>
</div>
</div>
<h3><span class="mw-headline" id=".E5.8F.82.E9.98.85">&#21442;&#38405;</span></h3>
<table class="t-dsc-begin">
<tr class="t-dsc">
<td>
<div class="t-dsc-member-div">
<div><a href="cpp-regex-regex_replace.html"><span class="t-lines"><span>regex_replace</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div>
</div>
</td>
<td>&#20197;&#26684;&#24335;&#21270;&#30340;&#26367;&#25442;&#25991;&#26412;&#26469;&#26367;&#25442;&#27491;&#21017;&#34920;&#36798;&#24335;&#21305;&#37197;&#30340;&#20986;&#29616;&#20301;&#32622;<br />
<span class="t-mark">(&#20989;&#25968;&#27169;&#26495;)</span></td>
</tr>
<tr class="t-dsc">
<td>
<div class="t-dsc-member-div">
<div><a href="cpp-regex-match_flag_type.html"><span class="t-lines"><span>match_flag_type</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div>
</div>
</td>
<td>&#29305;&#23450;&#20110;&#21305;&#37197;&#30340;&#36873;&#39033;<br />
<span class="t-mark">(typedef)</span></td>
</tr>
</table>
</div>
<div class="visualClear"></div>
</div>
</div>
</div>
</body>
</html>