mfc_example.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. * LOCATION: see http://www.boost.org for most recent version.
  13. * FILE mfc_example.cpp
  14. * VERSION see <boost/version.hpp>
  15. * DESCRIPTION: examples of using Boost.Regex with MFC and ATL string types.
  16. */
  17. #ifdef TEST_MFC
  18. #include <boost/regex/mfc.hpp>
  19. #include <cstringt.h>
  20. #include <atlstr.h>
  21. #include <assert.h>
  22. #include <tchar.h>
  23. #include <iostream>
  24. #ifdef _UNICODE
  25. #define cout wcout
  26. #endif
  27. //
  28. // Find out if *password* meets our password requirements,
  29. // as defined by the regular expression *requirements*.
  30. //
  31. bool is_valid_password(const CString& password, const CString& requirements)
  32. {
  33. return boost::regex_match(password, boost::make_regex(requirements));
  34. }
  35. //
  36. // Extract filename part of a path from a CString and return the result
  37. // as another CString:
  38. //
  39. CString get_filename(const CString& path)
  40. {
  41. boost::tregex r(__T("(?:\\A|.*\\\\)([^\\\\]+)"));
  42. boost::tmatch what;
  43. if(boost::regex_match(path, what, r))
  44. {
  45. // extract $1 as a CString:
  46. return CString(what[1].first, what.length(1));
  47. }
  48. else
  49. {
  50. throw std::runtime_error("Invalid pathname");
  51. }
  52. }
  53. CString extract_postcode(const CString& address)
  54. {
  55. // searches throw address for a UK postcode and returns the result,
  56. // the expression used is by Phil A. on www.regxlib.com:
  57. 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})$"));
  58. boost::tmatch what;
  59. if(boost::regex_search(address, what, r))
  60. {
  61. // extract $0 as a CString:
  62. return CString(what[0].first, what.length());
  63. }
  64. else
  65. {
  66. throw std::runtime_error("No postcode found");
  67. }
  68. }
  69. void enumerate_links(const CString& html)
  70. {
  71. // enumerate and print all the <a> links in some HTML text,
  72. // the expression used is by Andew Lee on www.regxlib.com:
  73. boost::tregex r(__T("href=[\"\']((http:\\/\\/|\\.\\/|\\/)?\\w+(\\.\\w+)*(\\/\\w+(\\.\\w+)?)*(\\/|\\?\\w*=\\w*(&\\w*=\\w*)*)?)[\"\']"));
  74. boost::tregex_iterator i(boost::make_regex_iterator(html, r)), j;
  75. while(i != j)
  76. {
  77. std::cout << (*i)[1] << std::endl;
  78. ++i;
  79. }
  80. }
  81. void enumerate_links2(const CString& html)
  82. {
  83. // enumerate and print all the <a> links in some HTML text,
  84. // the expression used is by Andew Lee on www.regxlib.com:
  85. boost::tregex r(__T("href=[\"\']((http:\\/\\/|\\.\\/|\\/)?\\w+(\\.\\w+)*(\\/\\w+(\\.\\w+)?)*(\\/|\\?\\w*=\\w*(&\\w*=\\w*)*)?)[\"\']"));
  86. boost::tregex_token_iterator i(boost::make_regex_token_iterator(html, r, 1)), j;
  87. while(i != j)
  88. {
  89. std::cout << *i << std::endl;
  90. ++i;
  91. }
  92. }
  93. //
  94. // Take a credit card number as a string of digits,
  95. // and reformat it as a human readable string with "-"
  96. // separating each group of four digits:
  97. //
  98. const boost::tregex e(__T("\\A(\\d{3,4})[- ]?(\\d{4})[- ]?(\\d{4})[- ]?(\\d{4})\\z"));
  99. const CString human_format = __T("$1-$2-$3-$4");
  100. CString human_readable_card_number(const CString& s)
  101. {
  102. return boost::regex_replace(s, e, human_format);
  103. }
  104. int main()
  105. {
  106. // password checks using regex_match:
  107. CString pwd = "abcDEF---";
  108. CString pwd_check = "(?=.*[[:lower:]])(?=.*[[:upper:]])(?=.*[[:punct:]]).{6,}";
  109. bool b = is_valid_password(pwd, pwd_check);
  110. assert(b);
  111. pwd = "abcD-";
  112. b = is_valid_password(pwd, pwd_check);
  113. assert(!b);
  114. // filename extraction with regex_match:
  115. CString file = "abc.hpp";
  116. file = get_filename(file);
  117. assert(file == "abc.hpp");
  118. file = "c:\\a\\b\\c\\d.h";
  119. file = get_filename(file);
  120. assert(file == "d.h");
  121. // postcode extraction with regex_search:
  122. CString address = "Joe Bloke, 001 Somestreet, Somewhere,\nPL2 8AB";
  123. CString postcode = extract_postcode(address);
  124. assert(postcode = "PL2 8NV");
  125. // html link extraction with regex_iterator:
  126. 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>";
  127. enumerate_links(text);
  128. enumerate_links2(text);
  129. CString credit_card_number = "1234567887654321";
  130. credit_card_number = human_readable_card_number(credit_card_number);
  131. assert(credit_card_number == "1234-5678-8765-4321");
  132. return 0;
  133. }
  134. #else
  135. #include <iostream>
  136. int main()
  137. {
  138. std::cout << "<NOTE>MFC support not enabled, feature unavailable</NOTE>";
  139. return 0;
  140. }
  141. #endif