2 Copyright 2006-2007 John Maddock.
3 Distributed under the Boost Software License, Version 1.0.
4 (See accompanying file LICENSE_1_0.txt or copy at
5 http://www.boost.org/LICENSE_1_0.txt).
8 [section:mfc_strings Using Boost Regex With MFC Strings]
10 [section:mfc_intro Introduction to Boost.Regex and MFC Strings]
12 The header `<boost/regex/mfc.hpp>` provides Boost.Regex support for MFC string
13 types: note that this support requires Visual Studio .NET (Visual C++ 7) or
14 later, where all of the MFC and ATL string types are based around the
15 CSimpleStringT class template.
17 In the following documentation, whenever you see
18 CSimpleStringT<charT>, then you can substitute any of the following
19 MFC/ATL types (all of which inherit from CSimpleStringT):
27 CStringT<charT,traits>
28 CFixedStringT<charT,N>
32 [section:mfc_regex_types Regex Types Used With MFC Strings]
34 The following typedefs are provided for the convenience of those working with
37 typedef basic_regex<TCHAR> tregex;
38 typedef match_results<TCHAR const*> tmatch;
39 typedef regex_iterator<TCHAR const*> tregex_iterator;
40 typedef regex_token_iterator<TCHAR const*> tregex_token_iterator;
42 If you are working with explicitly narrow or wide characters rather than
43 TCHAR, then use the regular Boost.Regex types `regex` and `wregex` instead.
46 [section:mfc_regex_create Regular Expression Creation From an MFC String]
48 The following helper function is available to assist in the creation of a
49 regular expression from an MFC/ATL string type:
51 template <class charT>
53 make_regex(const ATL::CSimpleStringT<charT>& s,
54 ::boost::regex_constants::syntax_option_type f = boost::regex_constants::normal);
56 [*Effects]: returns `basic_regex<charT>(s.GetString(), s.GetString() + s.GetLength(), f);`
59 [section:mfc_algo Overloaded Algorithms For MFC String Types]
61 For each regular expression algorithm that's overloaded for a `std::basic_string`
62 argument, there is also one overloaded for the MFC/ATL string types. These
63 algorithm signatures all look a lot more complex than they actually are,
64 but for completeness here they are anyway:
68 There are two overloads, the first reports what matched in a match_results
69 structure, the second does not.
71 All the usual caveats for [regex_match] apply, in particular the algorithm
72 will only report a successful match if all of the input text matches the
73 expression, if this isn't what you want then use [regex_search] instead.
75 template <class charT, class T, class A>
77 const ATL::CSimpleStringT<charT>& s,
78 match_results<const B*, A>& what,
79 const basic_regex<charT, T>& e,
80 boost::regex_constants::match_flag_type f = boost::regex_constants::match_default);
82 [*Effects]: returns `::boost::regex_match(s.GetString(), s.GetString() + s.GetLength(), what, e, f);`
87 // Extract filename part of a path from a CString and return the result
88 // as another CString:
90 CString get_filename(const CString& path)
92 boost::tregex r(__T("(?:\\A|.*\\\\)([^\\\\]+)"));
94 if(boost::regex_match(path, what, r))
96 // extract $1 as a CString:
97 return CString(what[1].first, what.length(1));
101 throw std::runtime_error("Invalid pathname");
105 [h4 regex_match (second overload)]
107 template <class charT, class T>
109 const ATL::CSimpleStringT<charT>& s,
110 const basic_regex<B, T>& e,
111 boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)
113 [*Effects]: returns `::boost::regex_match(s.GetString(), s.GetString() + s.GetLength(), e, f);`
118 // Find out if *password* meets our password requirements,
119 // as defined by the regular expression *requirements*.
121 bool is_valid_password(const CString& password, const CString& requirements)
123 return boost::regex_match(password, boost::make_regex(requirements));
128 There are two additional overloads for [regex_search], the first reports what
129 matched the second does not:
131 template <class charT, class A, class T>
132 bool regex_search(const ATL::CSimpleStringT<charT>& s,
133 match_results<const charT*, A>& what,
134 const basic_regex<charT, T>& e,
135 boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)
137 [*Effects]: returns ::boost::regex_search(s.GetString(), s.GetString() + s.GetLength(), what, e, f);
139 [*Example]: Postcode extraction from an address string.
141 CString extract_postcode(const CString& address)
143 // searches throw address for a UK postcode and returns the result,
144 // the expression used is by Phil A. on www.regxlib.com:
145 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})$"));
147 if(boost::regex_search(address, what, r))
149 // extract $0 as a CString:
150 return CString(what[0].first, what.length());
154 throw std::runtime_error("No postcode found");
158 [h4 regex_search (second overload)]
160 template <class charT, class T>
161 inline bool regex_search(const ATL::CSimpleStringT<charT>& s,
162 const basic_regex<charT, T>& e,
163 boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)
165 [*Effects]: returns `::boost::regex_search(s.GetString(), s.GetString() + s.GetLength(), e, f);`
169 There are two additional overloads for [regex_replace], the first sends output
170 to an output iterator, while the second creates a new string
172 template <class OutputIterator, class BidirectionalIterator, class traits, class
174 OutputIterator regex_replace(OutputIterator out,
175 BidirectionalIterator first,
176 BidirectionalIterator last,
177 const basic_regex<charT, traits>& e,
178 const ATL::CSimpleStringT<charT>& fmt,
179 match_flag_type flags = match_default)
181 [*Effects]: returns `::boost::regex_replace(out, first, last, e, fmt.GetString(), flags);`
183 template <class traits, charT>
184 ATL::CSimpleStringT<charT> regex_replace(const ATL::CSimpleStringT<charT>& s,
185 const basic_regex<charT, traits>& e,
186 const ATL::CSimpleStringT<charT>& fmt,
187 match_flag_type flags = match_default)
189 [*Effects]: returns a new string created using [regex_replace], and the same
190 memory manager as string /s/.
195 // Take a credit card number as a string of digits,
196 // and reformat it as a human readable string with "-"
197 // separating each group of four digits:
199 const boost::tregex e(__T("\\A(\\d{3,4})[- ]?(\\d{4})[- ]?(\\d{4})[- ]?(\\d{4})\\z"));
200 const CString human_format = __T("$1-$2-$3-$4");
202 CString human_readable_card_number(const CString& s)
204 return boost::regex_replace(s, e, human_format);
208 [section:mfc_iter Iterating Over the Matches Within An MFC String]
210 The following helper functions are provided to ease the conversion from an
211 MFC/ATL string to a [regex_iterator] or [regex_token_iterator]:
213 [h4 regex_iterator creation helper]
215 template <class charT>
216 regex_iterator<charT const*>
218 const ATL::CSimpleStringT<charT>& s,
219 const basic_regex<charT>& e,
220 ::boost::regex_constants::match_flag_type f = boost::regex_constants::match_default);
222 [*Effects]: returns `regex_iterator(s.GetString(), s.GetString() + s.GetLength(), e, f);`
226 void enumerate_links(const CString& html)
228 // enumerate and print all the links in some HTML text,
229 // the expression used is by Andew Lee on www.regxlib.com:
231 __T("href=[\"\']((http:\\/\\/|\\.\\/|\\/)?\\w+"
232 "(\\.\\w+)*(\\/\\w+(\\.\\w+)?)*"
233 "(\\/|\\?\\w*=\\w*(&\\w*=\\w*)*)?)[\"\']"));
234 boost::tregex_iterator i(boost::make_regex_iterator(html, r)), j;
237 std::cout << (*i)[1] << std::endl;
243 [h4 regex_token_iterator creation helpers]
245 template <class charT>
246 regex_token_iterator<charT const*>
247 make_regex_token_iterator(
248 const ATL::CSimpleStringT<charT>& s,
249 const basic_regex<charT>& e,
251 ::boost::regex_constants::match_flag_type f = boost::regex_constants::match_default);
253 [*Effects]: returns `regex_token_iterator(s.GetString(), s.GetString() + s.GetLength(), e, sub, f);`
255 template <class charT>
256 regex_token_iterator<charT const*>
257 make_regex_token_iterator(
258 const ATL::CSimpleStringT<charT>& s,
259 const basic_regex<charT>& e,
260 const std::vector<int>& subs,
261 ::boost::regex_constants::match_flag_type f = boost::regex_constants::match_default);
263 [*Effects]: returns `regex_token_iterator(s.GetString(), s.GetString() + s.GetLength(), e, subs, f);`
265 template <class charT, std::size_t N>
266 regex_token_iterator<charT const*>
267 make_regex_token_iterator(
268 const ATL::CSimpleStringT<charT>& s,
269 const basic_regex<charT>& e,
270 const int (& subs)[N],
271 ::boost::regex_constants::match_flag_type f = boost::regex_constants::match_default);
273 [*Effects]: returns `regex_token_iterator(s.GetString(), s.GetString() + s.GetLength(), e, subs, f);`
277 void enumerate_links2(const CString& html)
279 // enumerate and print all the links in some HTML text,
280 // the expression used is by Andew Lee on www.regxlib.com:
282 __T("href=[\"\']((http:\\/\\/|\\.\\/|\\/)?\\w+"
283 "(\\.\\w+)*(\\/\\w+(\\.\\w+)?)*"
284 "(\\/|\\?\\w*=\\w*(&\\w*=\\w*)*)?)[\"\']"));
285 boost::tregex_token_iterator i(boost::make_regex_token_iterator(html, r, 1)), j;
288 std::cout << *i << std::endl;