| 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 <boost/regex/mfc.hpp> 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 CSimpleStringT class template. </P> |
|---|
| 47 | <P>In the following documentation, whenever you see CSimpleStringT<charT>, |
|---|
| 48 | then you can substitute any of the following MFC/ATL types (all of which |
|---|
| 49 | inherit from CSimpleStringT):</P> |
|---|
| 50 | <P>CString<BR> |
|---|
| 51 | CStringA<BR> |
|---|
| 52 | CStringW<BR> |
|---|
| 53 | CAtlString<BR> |
|---|
| 54 | CAtlStringA<BR> |
|---|
| 55 | CAtlStringW<BR> |
|---|
| 56 | CStringT<charT,traits><BR> |
|---|
| 57 | CFixedStringT<charT,N><BR> |
|---|
| 58 | CSimpleStringT<charT></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><TCHAR> tregex; |
|---|
| 63 | typedef <A href="match_results.html" >match_results</A><TCHAR const*> tmatch; |
|---|
| 64 | typedef <A href="regex_iterator.html" >regex_iterator</A><TCHAR const*> tregex_iterator; |
|---|
| 65 | typedef <A href="regex_token_iterator.html" >regex_token_iterator</A><TCHAR const*> 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 <class charT> |
|---|
| 73 | basic_regex<charT> |
|---|
| 74 | make_regex(const ATL::CSimpleStringT<charT>& s, |
|---|
| 75 | ::boost::regex_constants::syntax_option_type f = boost::regex_constants::normal);</pre> |
|---|
| 76 | <P><STRONG>Effects</STRONG>: returns basic_regex<charT>(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. |
|---|
| 81 | These algorithm signatures 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. |
|---|
| 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 <class charT, class T, class A> |
|---|
| 92 | bool regex_match( |
|---|
| 93 | const ATL::CSimpleStringT<charT>& s, |
|---|
| 94 | match_results<const B*, A>& what, |
|---|
| 95 | const basic_regex<charT, T>& 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 | // |
|---|
| 105 | CString get_filename(const CString& 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 <class charT, class T> |
|---|
| 122 | bool regex_match( |
|---|
| 123 | const ATL::CSimpleStringT<charT>& s, |
|---|
| 124 | const basic_regex<B, T>& 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 | // |
|---|
| 134 | bool is_valid_password(const CString& password, const CString& 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 <class charT, class A, class T> |
|---|
| 143 | bool regex_search(const ATL::CSimpleStringT<charT>& s, |
|---|
| 144 | match_results<const charT*, A>& what, |
|---|
| 145 | const basic_regex<charT, T>& 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& 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 <class charT, class T> |
|---|
| 168 | inline bool regex_search(const ATL::CSimpleStringT<charT>& s, |
|---|
| 169 | const basic_regex<charT, T>& 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 <class OutputIterator, class BidirectionalIterator, class traits, class |
|---|
| 180 | charT> |
|---|
| 181 | OutputIterator regex_replace(OutputIterator out, |
|---|
| 182 | BidirectionalIterator first, |
|---|
| 183 | BidirectionalIterator last, |
|---|
| 184 | const basic_regex<charT, traits>& e, |
|---|
| 185 | const ATL::CSimpleStringT<charT>& 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 <class traits, charT> |
|---|
| 191 | ATL::CSimpleStringT<charT> regex_replace(const ATL::CSimpleStringT<charT>& s, |
|---|
| 192 | const basic_regex<charT, traits>& e, |
|---|
| 193 | const ATL::CSimpleStringT<charT>& 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 | // |
|---|
| 203 | const boost::tregex e(__T("\\A(\\d{3,4})[- ]?(\\d{4})[- ]?(\\d{4})[- ]?(\\d{4})\\z")); |
|---|
| 204 | const CString human_format = __T("$1-$2-$3-$4"); |
|---|
| 205 | |
|---|
| 206 | CString human_readable_card_number(const CString& 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 <class charT> |
|---|
| 217 | regex_iterator<charT const*> |
|---|
| 218 | make_regex_iterator( |
|---|
| 219 | const ATL::CSimpleStringT<charT>& s, |
|---|
| 220 | const basic_regex<charT>& 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& 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*(&\\w*=\\w*)*)?)[\"\']")); |
|---|
| 231 | boost::tregex_iterator i(boost::make_regex_iterator(html, r)), j; |
|---|
| 232 | while(i != j) |
|---|
| 233 | { |
|---|
| 234 | std::cout << (*i)[1] << 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 <class charT> |
|---|
| 242 | regex_token_iterator<charT const*> |
|---|
| 243 | make_regex_token_iterator( |
|---|
| 244 | const ATL::CSimpleStringT<charT>& s, |
|---|
| 245 | const basic_regex<charT>& 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 <class charT> |
|---|
| 252 | regex_token_iterator<charT const*> |
|---|
| 253 | make_regex_token_iterator( |
|---|
| 254 | const ATL::CSimpleStringT<charT>& s, |
|---|
| 255 | const basic_regex<charT>& e, |
|---|
| 256 | const std::vector<int>& 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 <class charT, std::size_t N> |
|---|
| 262 | regex_token_iterator<charT const*> |
|---|
| 263 | make_regex_token_iterator( |
|---|
| 264 | const ATL::CSimpleStringT<charT>& s, |
|---|
| 265 | const basic_regex<charT>& e, |
|---|
| 266 | const int (& 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& 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*(&\\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 << *i << std::endl; |
|---|
| 281 | ++i; |
|---|
| 282 | } |
|---|
| 283 | } </PRE> |
|---|
| 284 | <HR> |
|---|
| 285 | <p>Revised |
|---|
| 286 | <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan --> |
|---|
| 287 | 21 Dec 2004 |
|---|
| 288 | <!--webbot bot="Timestamp" endspan i-checksum="39359" --></p> |
|---|
| 289 | <p><i>© Copyright John Maddock 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> |
|---|