Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/regex/doc/mfc_strings.html @ 14

Last change on this file since 14 was 12, checked in by landauf, 17 years ago

added boost

File size: 14.6 KB
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<html>
3   <head>
4      <title>Boost.Regex: Working With MFC/ATL String Types</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">Working With MFC/ATL String Types.</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="#intro">Introduction</A> <dt><A href="#types">Types</A> <dt><A href="#create">Regular
28                     Expression Creation</A> <dt><A href="#algo">Overloaded Algorithms</A>
29                     <dd>
30                        <dl>
31                           <dt><A href="#regex_match">regex_match</A> <dt><A href="#regex_search">regex_search</A>
32                                 <dt><A href="#regex_replace">regex_replace</A> </dt>
33                        </dl>
34                        <dt><A href="#iterators">Iterators</A>
35                           <dd>
36                              <dl>
37                                 <dt><A href="#regex_iterator">regex_iterator creation helper</A> <dt><A href="#regex_token_iterator">
38                                          regex_token_iterator creation helpers</A></dt>
39                              </dl>
40                           </dd>
41      </dl>
42      <H3><a name="intro"></a>Introduction</H3>
43      <P>The header &lt;boost/regex/mfc.hpp&gt; provides Boost.Regex support for MFC
44         string types: note that this support requires Visual Studio .NET (Visual C++ 7)
45         or later, where all of the MFC and ATL string types are based around
46         the&nbsp;CSimpleStringT class template.&nbsp;</P>
47      <P>In the following documentation, whenever you see CSimpleStringT&lt;charT&gt;,
48         then you can substitute any of the following MFC/ATL types (all of which
49         inherit from&nbsp;CSimpleStringT):</P>
50      <P>CString<BR>
51         CStringA<BR>
52         CStringW<BR>
53         CAtlString<BR>
54         CAtlStringA<BR>
55         CAtlStringW<BR>
56         CStringT&lt;charT,traits&gt;<BR>
57         CFixedStringT&lt;charT,N&gt;<BR>
58         CSimpleStringT&lt;charT&gt;</B></P>
59      <H3><A name="types"></A>Types</H3>
60      <P>The following typedefs are provided for the convenience of those working with
61         TCHAR's:</P>
62      <PRE>typedef <A href="basic_regex.html" >basic_regex</A>&lt;TCHAR&gt; tregex;
63typedef <A href="match_results.html" >match_results</A>&lt;TCHAR const*&gt; tmatch;
64typedef <A href="regex_iterator.html" >regex_iterator</A>&lt;TCHAR const*&gt; tregex_iterator;
65typedef <A href="regex_token_iterator.html" >regex_token_iterator</A>&lt;TCHAR const*&gt; tregex_token_iterator;
66</PRE>
67      <P>If you are working with explicitly narrow or wide characters rather than TCHAR,
68         then use the regular Boost.Regex types instead.</P>
69      <H3><A name="create"></A>Regular Expression Creation</H3>
70      <P>The following helper function is available to assist in the creation of a
71         regular expression from an MFC/ATL string type:</P>
72      <pre>template &lt;class charT&gt;
73basic_regex&lt;charT&gt; 
74   make_regex(const ATL::CSimpleStringT&lt;charT&gt;&amp; s,
75              ::boost::regex_constants::syntax_option_type f = boost::regex_constants::normal);</pre>
76      <P><STRONG>Effects</STRONG>: returns basic_regex&lt;charT&gt;(s.GetString(),
77         s.GetString() + s.GetLength(), f);</P>
78      <H3><A name="algo"></A>Overloaded Algorithms</H3>
79      <P>For each regular expression algorithm that's overloaded for a std::basic_string
80         argument, there is also one overloaded for the MFC/ATL string types.&nbsp; 
81         These algorithm signatures&nbsp;all look a lot more complex than they actually
82         are, but for completeness here they are anyway:</P>
83      <H4><A name="regex_match"></A>regex_match</H4>
84      <P>There are two overloads, the first reports what matched in a match_results
85         structure, the second does not.&nbsp;
86      </P>
87      <P>All the usual caveats for <A href="regex_match.html">regex_match</A> apply, in
88         particular the algorithm will only report a successful match if <STRONG>all of the
89            input text matches the expression</STRONG>, if this isn't what you want then
90         use <A href="regex_search.html">regex_search</A> instead.</P>
91      <PRE>template &lt;class charT, class T, class A&gt;
92bool regex_match(
93   const ATL::CSimpleStringT&lt;charT&gt;&amp; s,
94   match_results&lt;const B*, A&gt;&amp; what,
95   const basic_regex&lt;charT, T&gt;&amp; e,
96   boost::regex_constants::match_flag_type f = boost::regex_constants::match_default); </PRE>
97      <P>
98      <P><STRONG>Effects</STRONG>: returns ::boost::<A href="regex_match.html">regex_match</A>(s.GetString(),
99         s.GetString() + s.GetLength(), what, e, f);</P>
100      <p><strong>Example:</strong></p>
101      <pre>//
102// Extract filename part of a path from a CString and return the result
103// as another CString:
104//
105CString get_filename(const CString&amp; path)
106{
107   boost::tregex r(__T("(?:\\A|.*\\\\)([^\\\\]+)"));
108   boost::tmatch what;
109   if(boost::regex_match(path, what, r))
110   {
111      // extract $1 as a CString:
112      return CString(what[1].first, what.length(1));
113   }
114   else
115   {
116      throw std::runtime_error("Invalid pathname");
117   }
118}
119      </pre>
120      <hr>
121      <PRE>template &lt;class charT, class T&gt;
122bool regex_match(
123   const ATL::CSimpleStringT&lt;charT&gt;&amp; s,
124   const basic_regex&lt;B, T&gt;&amp; e,
125   boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)</PRE>
126      <P>
127      <P><STRONG>Effects</STRONG>: returns ::boost::<A href="regex_match.html">regex_match</A>(s.GetString(),
128         s.GetString() + s.GetLength(), e, f);</P>
129      <p><strong>Example:</strong></p>
130      <pre>//
131// Find out if *password* meets our password requirements,
132// as defined by the regular expression *requirements*.
133//
134bool is_valid_password(const CString&amp; password, const CString&amp; requirements)
135{
136   return boost::regex_match(password, boost::make_regex(requirements));
137}      </pre>
138      <hr>
139      <H4><A name="regex_search"></A>regex_search</H4>
140      <P>There are two additional overloads for <A href="regex_search.html">regex_search</A>,
141         the first reports what matched the second does not:</P>
142      <PRE>template &lt;class charT, class A, class T&gt;
143bool regex_search(const ATL::CSimpleStringT&lt;charT&gt;&amp; s,
144                  match_results&lt;const charT*, A&gt;&amp; what,
145                  const basic_regex&lt;charT, T&gt;&amp; e,
146                  boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)</PRE>
147      <P><STRONG>Effects</STRONG>: returns ::boost::<A href="regex_search.html">regex_search</A>(s.GetString(),
148         s.GetString() + s.GetLength(), what, e, f);</P>
149      <P><STRONG>Example:</STRONG>: Postcode extraction from an address string.</P>
150      <pre>CString extract_postcode(const CString&amp; address)
151{
152   // searches throw address for a UK postcode and returns the result,
153   // the expression used is by Phil A. on www.regxlib.com:
154   boost::tregex r(__T("^(([A-Z]{1,2}[0-9]{1,2})|([A-Z]{1,2}[0-9][A-Z]))\\s?([0-9][A-Z]{2})$"));
155   boost::tmatch what;
156   if(boost::regex_search(address, what, r))
157   {
158      // extract $0 as a CString:
159      return CString(what[0].first, what.length());
160   }
161   else
162   {
163      throw std::runtime_error("No postcode found");
164   }
165}      </pre>
166      <hr>
167      <pre>template &lt;class charT, class T&gt;
168inline bool regex_search(const ATL::CSimpleStringT&lt;charT&gt;&amp; s,
169                 const basic_regex&lt;charT, T&gt;&amp; e,
170                 boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)
171</pre>
172      <P><STRONG>Effects</STRONG>: returns ::boost::<A href="regex_search.html">regex_search</A>(s.GetString(),
173         s.GetString() + s.GetLength(), e, f);</P>
174      <hr>
175      <H4><A name="regex_replace"></A>regex_replace</H4>
176      <P>There are two additional overloads for <A href="regex_replace.html">regex_replace</A>,
177         the first sends output to an output iterator, while the second creates a new
178         string</P>
179      <PRE>template &lt;class OutputIterator, class BidirectionalIterator, class traits, class
180          charT&gt;
181OutputIterator regex_replace(OutputIterator out,
182                           BidirectionalIterator first,
183                           BidirectionalIterator last,
184                           const basic_regex&lt;charT, traits&gt;&amp; e,
185                           const ATL::CSimpleStringT&lt;charT&gt;&amp; fmt,
186                           match_flag_type flags = match_default)
187</PRE>
188      <P><STRONG>Effects</STRONG>: returns ::boost::<A href="regex_replace.html">regex_replace</A>(out,
189         first, last, e, fmt.GetString(), flags);</P>
190      <pre>template &lt;class traits, charT&gt;
191ATL::CSimpleStringT&lt;charT&gt; regex_replace(const ATL::CSimpleStringT&lt;charT&gt;&amp; s,
192                            const basic_regex&lt;charT, traits&gt;&amp; e,
193                            const ATL::CSimpleStringT&lt;charT&gt;&amp; fmt,
194                            match_flag_type flags = match_default)</pre>
195      <P><STRONG>Effects</STRONG>: returns a new string created using <A href="regex_replace.html">
196            regex_replace</A>, and the same memory manager as string <EM>s</EM>.</P>
197      <P><STRONG>Example:</STRONG></P>
198      <PRE>//
199// Take a credit card number as a string of digits,
200// and reformat it as a human readable string with "-"
201// separating each group of four digits:
202//
203const boost::tregex e(__T("\\A(\\d{3,4})[- ]?(\\d{4})[- ]?(\\d{4})[- ]?(\\d{4})\\z"));
204const CString human_format = __T("$1-$2-$3-$4");
205
206CString human_readable_card_number(const CString&amp; s)
207{
208   return boost::regex_replace(s, e, human_format);
209}
210      </PRE>
211      <H3><a name="iterators"></a>Iterators</H3>
212      <P>The following helper functions are provided to ease the conversion from an
213         MFC/ATL string to a <A href="regex_iterator.html">regex_iterator</A> or <A href="regex_token_iterator.html">
214            regex_token_iterator</A>:</P>
215      <H4><A name="regex_iterator"></A>regex_iterator creation helper</H4>
216      <PRE>template &lt;class charT&gt;
217regex_iterator&lt;charT const*&gt; 
218   make_regex_iterator(
219      const ATL::CSimpleStringT&lt;charT&gt;&amp; s,
220      const basic_regex&lt;charT&gt;&amp; e,
221      ::boost::regex_constants::match_flag_type f = boost::regex_constants::match_default);
222</PRE>
223      <p><STRONG>Effects:</STRONG>returns <A href="regex_iterator.html">regex_iterator</A>(s.GetString(),
224         s.GetString() + s.GetLength(), e, f);</p>
225      <p><strong>Example:</strong></p>
226      <pre>void enumerate_links(const CString&amp; html)
227{
228   // enumerate and print all the <a> links in some HTML text,
229   // the expression used is by Andew Lee on <a href="http://www.regxlib.com">www.regxlib.com</a>:
230   boost::tregex r(__T("href=[\"\']((http:\\/\\/|\\.\\/|\\/)?\\w+(\\.\\w+)*(\\/\\w+(\\.\\w+)?)*(\\/|\\?\\w*=\\w*(&amp;\\w*=\\w*)*)?)[\"\']"));
231   boost::tregex_iterator i(boost::make_regex_iterator(html, r)), j;
232   while(i != j)
233   {
234      std::cout &lt;&lt; (*i)[1] &lt;&lt; std::endl;
235      ++i;
236   }
237}
238      </pre>
239      <hr>
240      <H4><A name="regex_token_iterator"></A>regex_token_iterator creation helpers</H4>
241      <PRE>template &lt;class charT&gt; 
242regex_token_iterator&lt;charT const*&gt; 
243   make_regex_token_iterator(
244      const ATL::CSimpleStringT&lt;charT&gt;&amp; s,
245      const basic_regex&lt;charT&gt;&amp; e,
246      int sub = 0,
247      ::boost::regex_constants::match_flag_type f = boost::regex_constants::match_default);
248</PRE>
249      <p><STRONG>Effects:</STRONG>returns <A href="regex_token_iterator.html">regex_token_iterator</A>(s.GetString(),
250         s.GetString() + s.GetLength(), e, sub, f);</p>
251      <pre>template &lt;class charT&gt; 
252regex_token_iterator&lt;charT const*&gt; 
253   make_regex_token_iterator(
254      const ATL::CSimpleStringT&lt;charT&gt;&amp; s,
255      const basic_regex&lt;charT&gt;&amp; e,
256      const std::vector&lt;int&gt;&amp; subs,
257      ::boost::regex_constants::match_flag_type f = boost::regex_constants::match_default);
258</pre>
259      <p><STRONG>Effects:</STRONG>returns <A href="regex_token_iterator.html">regex_token_iterator</A>(s.GetString(),
260         s.GetString() + s.GetLength(), e, subs, f);</p>
261      <pre>template &lt;class charT, std::size_t N&gt; 
262regex_token_iterator&lt;charT const*&gt; 
263   make_regex_token_iterator(
264      const ATL::CSimpleStringT&lt;charT&gt;&amp; s,
265      const basic_regex&lt;charT&gt;&amp; e,
266      const int (&amp; subs)[N],
267      ::boost::regex_constants::match_flag_type f = boost::regex_constants::match_default);
268</pre>
269      <p><STRONG>Effects: </STRONG>returns <A href="regex_token_iterator.html">regex_token_iterator</A>(s.GetString(),
270         s.GetString() + s.GetLength(), e, subs, f);</p>
271      <P><STRONG>Example:</STRONG></P>
272      <PRE>void enumerate_links2(const CString&amp; html)
273{
274   // enumerate and print all the <a> links in some HTML text,
275   // the expression used is by Andew Lee on <a href="http://www.regxlib.com">www.regxlib.com</a>:
276   boost::tregex r(__T("href=[\"\']((http:\\/\\/|\\.\\/|\\/)?\\w+(\\.\\w+)*(\\/\\w+(\\.\\w+)?)*(\\/|\\?\\w*=\\w*(&amp;\\w*=\\w*)*)?)[\"\']"));
277   boost::tregex_token_iterator i(boost::make_regex_token_iterator(html, r, 1)), j;
278   while(i != j)
279   {
280      std::cout &lt;&lt; *i &lt;&lt; std::endl;
281      ++i;
282   }
283}      </PRE>
284      <HR>
285      <p>Revised&nbsp; 
286         <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan --> 
287         21&nbsp;Dec 2004
288         <!--webbot bot="Timestamp" endspan i-checksum="39359" --></p>
289      <p><i>© Copyright John Maddock&nbsp;2004</i></p>
290      <P><I>Use, modification and distribution are subject to the Boost Software License,
291            Version 1.0. (See accompanying file <A href="../../../LICENSE_1_0.txt">LICENSE_1_0.txt</A>
292            or copy at <A href="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</A>)</I></P>
293   </body>
294</html>
Note: See TracBrowser for help on using the repository browser.