regex_split_example_1.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. *
  3. * Copyright (c) 1998-2002
  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 regex_split_example_1.cpp
  14. * VERSION see <boost/version.hpp>
  15. * DESCRIPTION: regex_split example: split a string into tokens.
  16. */
  17. #include <boost/regex.hpp>
  18. #include <list>
  19. unsigned tokenise(std::list<std::string>& l, std::string& s)
  20. {
  21. return boost::regex_split(std::back_inserter(l), s);
  22. }
  23. #include <iostream>
  24. using namespace std;
  25. #if defined(BOOST_MSVC) || (defined(__BORLANDC__) && (__BORLANDC__ == 0x550))
  26. //
  27. // problem with std::getline under MSVC6sp3
  28. istream& getline(istream& is, std::string& s)
  29. {
  30. s.erase();
  31. char c = static_cast<char>(is.get());
  32. while(c != '\n')
  33. {
  34. s.append(1, c);
  35. c = static_cast<char>(is.get());
  36. }
  37. return is;
  38. }
  39. #endif
  40. int main(int argc, const char*[])
  41. {
  42. string s;
  43. list<string> l;
  44. do{
  45. if(argc == 1)
  46. {
  47. cout << "Enter text to split (or \"quit\" to exit): ";
  48. getline(cin, s);
  49. if(s == "quit") break;
  50. }
  51. else
  52. s = "This is a string of tokens";
  53. unsigned result = tokenise(l, s);
  54. cout << result << " tokens found" << endl;
  55. cout << "The remaining text is: \"" << s << "\"" << endl;
  56. while(l.size())
  57. {
  58. s = *(l.begin());
  59. l.pop_front();
  60. cout << s << endl;
  61. }
  62. }while(argc == 1);
  63. return 0;
  64. }