Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/spirit/example/fundamental/regular_expression.cpp @ 12

Last change on this file since 12 was 12, checked in by landauf, 17 years ago

added boost

File size: 3.5 KB
Line 
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/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/utility/regex.hpp>
42
43///////////////////////////////////////////////////////////////////////////////
44//  used namespaces
45using namespace std;
46using namespace boost::spirit;
47
48///////////////////////////////////////////////////////////////////////////////
49// main entry point
50int main (int argc, char *argv[])
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
Note: See TracBrowser for help on using the repository browser.