]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /* |
2 | * | |
1e59de90 | 3 | * Copyright (c) 2003 |
7c673cae FG |
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 regex_token_iterator_example_1.cpp | |
15 | * VERSION see <boost/version.hpp> | |
16 | * DESCRIPTION: regex_token_iterator example: split a string into tokens. | |
17 | */ | |
18 | ||
19 | ||
20 | #include <boost/regex.hpp> | |
21 | ||
22 | #include <iostream> | |
23 | using namespace std; | |
24 | ||
25 | ||
20effc67 | 26 | #if defined(BOOST_MSVC) || (defined(BOOST_BORLANDC) && (BOOST_BORLANDC == 0x550)) |
7c673cae FG |
27 | // |
28 | // problem with std::getline under MSVC6sp3 | |
29 | istream& getline(istream& is, std::string& s) | |
30 | { | |
31 | s.erase(); | |
32 | char c = static_cast<char>(is.get()); | |
33 | while(c != '\n') | |
34 | { | |
35 | s.append(1, c); | |
36 | c = static_cast<char>(is.get()); | |
37 | } | |
38 | return is; | |
39 | } | |
40 | #endif | |
41 | ||
42 | ||
43 | int main(int argc, const char*[]) | |
44 | { | |
45 | string s; | |
46 | do{ | |
47 | if(argc == 1) | |
48 | { | |
49 | cout << "Enter text to split (or \"quit\" to exit): "; | |
50 | getline(cin, s); | |
51 | if(s == "quit") break; | |
52 | } | |
53 | else | |
54 | s = "This is a string of tokens"; | |
55 | ||
56 | boost::regex re("\\s+"); | |
57 | boost::sregex_token_iterator i(s.begin(), s.end(), re, -1); | |
58 | boost::sregex_token_iterator j; | |
59 | ||
60 | unsigned count = 0; | |
61 | while(i != j) | |
62 | { | |
63 | cout << *i++ << endl; | |
64 | count++; | |
65 | } | |
66 | cout << "There were " << count << " tokens found." << endl; | |
67 | ||
68 | }while(argc == 1); | |
69 | return 0; | |
70 | } | |
71 | ||
72 |