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