6 * Use, modification and distribution are subject to the
7 * Boost Software License, Version 1.0. (See accompanying file
8 * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
13 * LOCATION: see http://www.boost.org for most recent version.
14 * FILE mfc_example.cpp
15 * VERSION see <boost/version.hpp>
16 * DESCRIPTION: examples of using Boost.Regex with MFC and ATL string types.
21 #include <boost/regex/mfc.hpp>
33 // Find out if *password* meets our password requirements,
34 // as defined by the regular expression *requirements*.
36 bool is_valid_password(const CString
& password
, const CString
& requirements
)
38 return boost::regex_match(password
, boost::make_regex(requirements
));
42 // Extract filename part of a path from a CString and return the result
43 // as another CString:
45 CString
get_filename(const CString
& path
)
47 boost::tregex
r(__T("(?:\\A|.*\\\\)([^\\\\]+)"));
49 if(boost::regex_match(path
, what
, r
))
51 // extract $1 as a CString:
52 return CString(what
[1].first
, what
.length(1));
56 throw std::runtime_error("Invalid pathname");
60 CString
extract_postcode(const CString
& address
)
62 // searches throw address for a UK postcode and returns the result,
63 // the expression used is by Phil A. on www.regxlib.com:
64 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})$"));
66 if(boost::regex_search(address
, what
, r
))
68 // extract $0 as a CString:
69 return CString(what
[0].first
, what
.length());
73 throw std::runtime_error("No postcode found");
77 void enumerate_links(const CString
& html
)
79 // enumerate and print all the <a> links in some HTML text,
80 // the expression used is by Andew Lee on www.regxlib.com:
81 boost::tregex
r(__T("href=[\"\']((http:\\/\\/|\\.\\/|\\/)?\\w+(\\.\\w+)*(\\/\\w+(\\.\\w+)?)*(\\/|\\?\\w*=\\w*(&\\w*=\\w*)*)?)[\"\']"));
82 boost::tregex_iterator
i(boost::make_regex_iterator(html
, r
)), j
;
85 std::cout
<< (*i
)[1] << std::endl
;
90 void enumerate_links2(const CString
& html
)
92 // enumerate and print all the <a> links in some HTML text,
93 // the expression used is by Andew Lee on www.regxlib.com:
94 boost::tregex
r(__T("href=[\"\']((http:\\/\\/|\\.\\/|\\/)?\\w+(\\.\\w+)*(\\/\\w+(\\.\\w+)?)*(\\/|\\?\\w*=\\w*(&\\w*=\\w*)*)?)[\"\']"));
95 boost::tregex_token_iterator
i(boost::make_regex_token_iterator(html
, r
, 1)), j
;
98 std::cout
<< *i
<< std::endl
;
104 // Take a credit card number as a string of digits,
105 // and reformat it as a human readable string with "-"
106 // separating each group of four digits:
108 const boost::tregex
e(__T("\\A(\\d{3,4})[- ]?(\\d{4})[- ]?(\\d{4})[- ]?(\\d{4})\\z"));
109 const CString human_format
= __T("$1-$2-$3-$4");
111 CString
human_readable_card_number(const CString
& s
)
113 return boost::regex_replace(s
, e
, human_format
);
119 // password checks using regex_match:
120 CString pwd
= "abcDEF---";
121 CString pwd_check
= "(?=.*[[:lower:]])(?=.*[[:upper:]])(?=.*[[:punct:]]).{6,}";
122 bool b
= is_valid_password(pwd
, pwd_check
);
125 b
= is_valid_password(pwd
, pwd_check
);
128 // filename extraction with regex_match:
129 CString file
= "abc.hpp";
130 file
= get_filename(file
);
131 assert(file
== "abc.hpp");
132 file
= "c:\\a\\b\\c\\d.h";
133 file
= get_filename(file
);
134 assert(file
== "d.h");
136 // postcode extraction with regex_search:
137 CString address
= "Joe Bloke, 001 Somestreet, Somewhere,\nPL2 8AB";
138 CString postcode
= extract_postcode(address
);
139 assert(postcode
= "PL2 8NV");
141 // html link extraction with regex_iterator:
142 CString text
= "<dt><a href=\"syntax_perl.html\">Perl Regular Expressions</a></dt><dt><a href=\"syntax_extended.html\">POSIX-Extended Regular Expressions</a></dt><dt><a href=\"syntax_basic.html\">POSIX-Basic Regular Expressions</a></dt>";
143 enumerate_links(text
);
144 enumerate_links2(text
);
146 CString credit_card_number
= "1234567887654321";
147 credit_card_number
= human_readable_card_number(credit_card_number
);
148 assert(credit_card_number
== "1234-5678-8765-4321");
158 std::cout
<< "<NOTE>MFC support not enabled, feature unavailable</NOTE>";