regex_split.qbk 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. [/
  2. Copyright 2006-2007 John Maddock.
  3. Distributed under the Boost Software License, Version 1.0.
  4. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt).
  6. ]
  7. [section:regex_split regex_split (deprecated)]
  8. The algorithm [regex_split] has been deprecated in favor of the iterator
  9. [regex_token_iterator] which has a more flexible and powerful interface,
  10. as well as following the more usual standard library "pull" rather than
  11. "push" semantics.
  12. Code which uses [regex_split] will continue to compile, the following
  13. documentation is taken from a previous Boost.Regex version:
  14. #include <boost/regex.hpp>
  15. Algorithm [regex_split] performs a similar operation to the perl split operation,
  16. and comes in three overloaded forms:
  17. template <class OutputIterator, class charT, class Traits1, class Alloc1, class Traits2>
  18. std::size_t regex_split(OutputIterator out,
  19. std::basic_string<charT, Traits1, Alloc1>& s,
  20. const basic_regex<charT, Traits2>& e,
  21. boost::match_flag_type flags,
  22. std::size_t max_split);
  23. template <class OutputIterator, class charT, class Traits1, class Alloc1, class Traits2>
  24. std::size_t regex_split(OutputIterator out,
  25. std::basic_string<charT, Traits1, Alloc1>& s,
  26. const basic_regex<charT, Traits2>& e,
  27. boost::match_flag_type flags = match_default);
  28. template <class OutputIterator, class charT, class Traits1, class Alloc1>
  29. std::size_t regex_split(OutputIterator out,
  30. std::basic_string<charT, Traits1, Alloc1>& s);
  31. [*Effects]: Each version of the algorithm takes an output-iterator for
  32. output, and a string for input. If the expression contains no marked
  33. sub-expressions, then the algorithm writes one string onto the output-iterator
  34. for each section of input that does not match the expression.
  35. If the expression does contain marked sub-expressions, then each
  36. time a match is found, one string for each marked sub-expression will be
  37. written to the output-iterator. No more than max_split strings will be written
  38. to the output-iterator. Before returning, all the input processed will be
  39. deleted from the string /s/ (if /max_split/ is not reached then all of /s/
  40. will be deleted). Returns the number of strings written to the output-iterator.
  41. If the parameter /max_split/ is not specified then it defaults to `UINT_MAX`.
  42. If no expression is specified, then it defaults to "\\s+", and splitting occurs
  43. on whitespace.
  44. [*Throws]: `std::runtime_error` if the complexity of matching the expression
  45. against an N character string begins to exceed O(N[super 2]), or if the
  46. program runs out of stack space while matching the expression (if Boost.Regex is
  47. configured in recursive mode), or if the matcher exhausts its permitted
  48. memory allocation (if Boost.Regex is configured in non-recursive mode).
  49. [*Example]: the following function will split the input string into a
  50. series of tokens, and remove each token from the string /s/:
  51. unsigned tokenise(std::list<std::string>& l, std::string& s)
  52. {
  53. return boost::regex_split(std::back_inserter(l), s);
  54. }
  55. Example: the following short program will extract all of the URL's
  56. from a html file, and print them out to cout:
  57. #include <list>
  58. #include <fstream>
  59. #include <iostream>
  60. #include <boost/regex.hpp>
  61. boost::regex e("<\\s*A\\s+[^>]*href\\s*=\\s*\"([^\"]*)\"",
  62. boost::regbase::normal | boost::regbase::icase);
  63. void load_file(std::string& s, std::istream& is)
  64. {
  65. s.erase();
  66. //
  67. // attempt to grow string buffer to match file size,
  68. // this doesn't always work...
  69. s.reserve(is.rdbuf()-&gtin_avail());
  70. char c;
  71. while(is.get(c))
  72. {
  73. // use logarithmic growth strategy, in case
  74. // in_avail (above) returned zero:
  75. if(s.capacity() == s.size())
  76. s.reserve(s.capacity() * 3);
  77. s.append(1, c);
  78. }
  79. }
  80. int main(int argc, char** argv)
  81. {
  82. std::string s;
  83. std::list<std::string> l;
  84. for(int i = 1; i < argc; ++i)
  85. {
  86. std::cout << "Findings URL's in " << argv[i] << ":" << std::endl;
  87. s.erase();
  88. std::ifstream is(argv[i]);
  89. load_file(s, is);
  90. boost::regex_split(std::back_inserter(l), s, e);
  91. while(l.size())
  92. {
  93. s = *(l.begin());
  94. l.pop_front();
  95. std::cout << s << std::endl;
  96. }
  97. }
  98. return 0;
  99. }
  100. [endsect]