1 | /* |
---|
2 | * |
---|
3 | * Copyright (c) 2004 |
---|
4 | * John Maddock |
---|
5 | * |
---|
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) |
---|
9 | * |
---|
10 | */ |
---|
11 | |
---|
12 | /* |
---|
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. |
---|
17 | */ |
---|
18 | |
---|
19 | #ifdef TEST_MFC |
---|
20 | |
---|
21 | #include <boost/regex/mfc.hpp> |
---|
22 | #include <cstringt.h> |
---|
23 | #include <atlstr.h> |
---|
24 | #include <assert.h> |
---|
25 | #include <tchar.h> |
---|
26 | #include <iostream> |
---|
27 | |
---|
28 | #ifdef _UNICODE |
---|
29 | #define cout wcout |
---|
30 | #endif |
---|
31 | |
---|
32 | // |
---|
33 | // Find out if *password* meets our password requirements, |
---|
34 | // as defined by the regular expression *requirements*. |
---|
35 | // |
---|
36 | bool is_valid_password(const CString& password, const CString& requirements) |
---|
37 | { |
---|
38 | return boost::regex_match(password, boost::make_regex(requirements)); |
---|
39 | } |
---|
40 | |
---|
41 | // |
---|
42 | // Extract filename part of a path from a CString and return the result |
---|
43 | // as another CString: |
---|
44 | // |
---|
45 | CString get_filename(const CString& path) |
---|
46 | { |
---|
47 | boost::tregex r(__T("(?:\\A|.*\\\\)([^\\\\]+)")); |
---|
48 | boost::tmatch what; |
---|
49 | if(boost::regex_match(path, what, r)) |
---|
50 | { |
---|
51 | // extract $1 as a CString: |
---|
52 | return CString(what[1].first, what.length(1)); |
---|
53 | } |
---|
54 | else |
---|
55 | { |
---|
56 | throw std::runtime_error("Invalid pathname"); |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | CString extract_postcode(const CString& address) |
---|
61 | { |
---|
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})$")); |
---|
65 | boost::tmatch what; |
---|
66 | if(boost::regex_search(address, what, r)) |
---|
67 | { |
---|
68 | // extract $0 as a CString: |
---|
69 | return CString(what[0].first, what.length()); |
---|
70 | } |
---|
71 | else |
---|
72 | { |
---|
73 | throw std::runtime_error("No postcode found"); |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | void enumerate_links(const CString& html) |
---|
78 | { |
---|
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; |
---|
83 | while(i != j) |
---|
84 | { |
---|
85 | std::cout << (*i)[1] << std::endl; |
---|
86 | ++i; |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | void enumerate_links2(const CString& html) |
---|
91 | { |
---|
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; |
---|
96 | while(i != j) |
---|
97 | { |
---|
98 | std::cout << *i << std::endl; |
---|
99 | ++i; |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | // |
---|
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: |
---|
107 | // |
---|
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"); |
---|
110 | |
---|
111 | CString human_readable_card_number(const CString& s) |
---|
112 | { |
---|
113 | return boost::regex_replace(s, e, human_format); |
---|
114 | } |
---|
115 | |
---|
116 | |
---|
117 | int main() |
---|
118 | { |
---|
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); |
---|
123 | assert(b); |
---|
124 | pwd = "abcD-"; |
---|
125 | b = is_valid_password(pwd, pwd_check); |
---|
126 | assert(!b); |
---|
127 | |
---|
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"); |
---|
135 | |
---|
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"); |
---|
140 | |
---|
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); |
---|
145 | |
---|
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"); |
---|
149 | return 0; |
---|
150 | } |
---|
151 | |
---|
152 | #else |
---|
153 | |
---|
154 | #include <iostream> |
---|
155 | |
---|
156 | int main() |
---|
157 | { |
---|
158 | std::cout << "<NOTE>MFC support not enabled, feature unavailable</NOTE>"; |
---|
159 | return 0; |
---|
160 | } |
---|
161 | |
---|
162 | #endif |
---|