regular_expression.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*=============================================================================
  2. Copyright (c) 2002-2003 Hartmut Kaiser
  3. http://spirit.sourceforge.net/
  4. Use, modification and distribution is subject to the Boost Software
  5. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. ///////////////////////////////////////////////////////////////////////////////
  9. //
  10. // Demonstrate regular expression parser objects
  11. // See the "Regular Expression Parser" chapter in the User's Guide.
  12. //
  13. // This sample requires an installed version of the boost regex library
  14. // (http://www.boost.org) The sample was tested with boost V1.28.0
  15. //
  16. ///////////////////////////////////////////////////////////////////////////////
  17. #include <string>
  18. #include <iostream>
  19. #include <boost/version.hpp>
  20. #include <boost/spirit/include/classic_core.hpp>
  21. ///////////////////////////////////////////////////////////////////////////////
  22. //
  23. // The following header must be included, if regular expression support is
  24. // required for Spirit.
  25. //
  26. // The BOOST_SPIRIT_NO_REGEX_LIB PP constant should be defined, if you're
  27. // using the Boost.Regex library from one translation unit only. Otherwise
  28. // you have to link with the Boost.Regex library as defined in the related
  29. // documentation (see. http://www.boost.org).
  30. //
  31. // For Boost > V1.32.0 you'll always have to link against the Boost.Regex
  32. // libraries.
  33. //
  34. ///////////////////////////////////////////////////////////////////////////////
  35. #if BOOST_VERSION <= 103200
  36. #define BOOST_SPIRIT_NO_REGEX_LIB
  37. #endif
  38. #include <boost/spirit/include/classic_regex.hpp>
  39. ///////////////////////////////////////////////////////////////////////////////
  40. // used namespaces
  41. using namespace std;
  42. using namespace BOOST_SPIRIT_CLASSIC_NS;
  43. ///////////////////////////////////////////////////////////////////////////////
  44. // main entry point
  45. int main()
  46. {
  47. const char *ptest = "123 E 456";
  48. const char *prx = "[1-9]+[[:space:]]*E[[:space:]]*";
  49. cout << "Parse " << ptest << " against regular expression: " << prx
  50. << endl;
  51. // 1. direct use of rxlit<>
  52. rxstrlit<> regexpr(prx);
  53. parse_info<> result;
  54. string str;
  55. result = parse (ptest, regexpr[assign(str)]);
  56. if (result.hit)
  57. {
  58. cout << "Parsed regular expression successfully!" << endl;
  59. cout << "Matched (" << (int)result.length << ") characters: ";
  60. cout << "\"" << str << "\"" << endl;
  61. }
  62. else
  63. {
  64. cout << "Failed to parse regular expression!" << endl;
  65. }
  66. cout << endl;
  67. // 2. use of regex_p predefined parser object
  68. str.empty();
  69. result = parse (ptest, regex_p(prx)[assign(str)]);
  70. if (result.hit)
  71. {
  72. cout << "Parsed regular expression successfully!" << endl;
  73. cout << "Matched (" << (int)result.length << ") characters: ";
  74. cout << "\"" << str << "\"" << endl;
  75. }
  76. else
  77. {
  78. cout << "Failed to parse regular expression!" << endl;
  79. }
  80. cout << endl;
  81. // 3. test the regression reported by Grzegorz Marcin Koczyk (gkoczyk@echostar.pl)
  82. string str1;
  83. string str2;
  84. char const *ptest1 = "Token whatever \nToken";
  85. result = parse(ptest1, rxstrlit<>("Token")[assign(str1)]
  86. >> rxstrlit<>("Token")[assign(str2)]);
  87. if (!result.hit)
  88. cout << "Parsed regular expression successfully!" << endl;
  89. else
  90. cout << "Failed to parse regular expression!" << endl;
  91. cout << endl;
  92. return 0;
  93. }