tokenization.qbk 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. [/
  2. / Copyright (c) 2008 Eric Niebler
  3. /
  4. / Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. /]
  7. [section String Splitting and Tokenization]
  8. _regex_token_iterator_ is the Ginsu knife of the text manipulation world. It slices! It dices! This section describes
  9. how to use the highly-configurable _regex_token_iterator_ to chop up input sequences.
  10. [h2 Overview]
  11. You initialize a _regex_token_iterator_ with an input sequence, a regex, and some optional configuration parameters.
  12. The _regex_token_iterator_ will use _regex_search_ to find the first place in the sequence that the regex matches. When
  13. dereferenced, the _regex_token_iterator_ returns a ['token] in the form of a `std::basic_string<>`. Which string it returns
  14. depends on the configuration parameters. By default it returns a string corresponding to the full match, but it could also
  15. return a string corresponding to a particular marked sub-expression, or even the part of the sequence that ['didn't] match.
  16. When you increment the _regex_token_iterator_, it will move to the next token. Which token is next depends on the configuration
  17. parameters. It could simply be a different marked sub-expression in the current match, or it could be part or all of the
  18. next match. Or it could be the part that ['didn't] match.
  19. As you can see, _regex_token_iterator_ can do a lot. That makes it hard to describe, but some examples should make it clear.
  20. [h2 Example 1: Simple Tokenization]
  21. This example uses _regex_token_iterator_ to chop a sequence into a series of tokens consisting of words.
  22. std::string input("This is his face");
  23. sregex re = +_w; // find a word
  24. // iterate over all the words in the input
  25. sregex_token_iterator begin( input.begin(), input.end(), re ), end;
  26. // write all the words to std::cout
  27. std::ostream_iterator< std::string > out_iter( std::cout, "\n" );
  28. std::copy( begin, end, out_iter );
  29. This program displays the following:
  30. [pre
  31. This
  32. is
  33. his
  34. face
  35. ]
  36. [h2 Example 2: Simple Tokenization, Reloaded]
  37. This example also uses _regex_token_iterator_ to chop a sequence into a series of tokens consisting of words,
  38. but it uses the regex as a delimiter. When we pass a `-1` as the last parameter to the _regex_token_iterator_
  39. constructor, it instructs the token iterator to consider as tokens those parts of the input that ['didn't]
  40. match the regex.
  41. std::string input("This is his face");
  42. sregex re = +_s; // find white space
  43. // iterate over all non-white space in the input. Note the -1 below:
  44. sregex_token_iterator begin( input.begin(), input.end(), re, -1 ), end;
  45. // write all the words to std::cout
  46. std::ostream_iterator< std::string > out_iter( std::cout, "\n" );
  47. std::copy( begin, end, out_iter );
  48. This program displays the following:
  49. [pre
  50. This
  51. is
  52. his
  53. face
  54. ]
  55. [h2 Example 3: Simple Tokenization, Revolutions]
  56. This example also uses _regex_token_iterator_ to chop a sequence containing a bunch of dates into a series of
  57. tokens consisting of just the years. When we pass a positive integer [^['N]] as the last parameter to the
  58. _regex_token_iterator_ constructor, it instructs the token iterator to consider as tokens only the [^['N]]-th
  59. marked sub-expression of each match.
  60. std::string input("01/02/2003 blahblah 04/23/1999 blahblah 11/13/1981");
  61. sregex re = sregex::compile("(\\d{2})/(\\d{2})/(\\d{4})"); // find a date
  62. // iterate over all the years in the input. Note the 3 below, corresponding to the 3rd sub-expression:
  63. sregex_token_iterator begin( input.begin(), input.end(), re, 3 ), end;
  64. // write all the words to std::cout
  65. std::ostream_iterator< std::string > out_iter( std::cout, "\n" );
  66. std::copy( begin, end, out_iter );
  67. This program displays the following:
  68. [pre
  69. 2003
  70. 1999
  71. 1981
  72. ]
  73. [h2 Example 4: Not-So-Simple Tokenization]
  74. This example is like the previous one, except that instead of tokenizing just the years, this program
  75. turns the days, months and years into tokens. When we pass an array of integers [^['{I,J,...}]] as the last
  76. parameter to the _regex_token_iterator_ constructor, it instructs the token iterator to consider as tokens the
  77. [^['I]]-th, [^['J]]-th, etc. marked sub-expression of each match.
  78. std::string input("01/02/2003 blahblah 04/23/1999 blahblah 11/13/1981");
  79. sregex re = sregex::compile("(\\d{2})/(\\d{2})/(\\d{4})"); // find a date
  80. // iterate over the days, months and years in the input
  81. int const sub_matches[] = { 2, 1, 3 }; // day, month, year
  82. sregex_token_iterator begin( input.begin(), input.end(), re, sub_matches ), end;
  83. // write all the words to std::cout
  84. std::ostream_iterator< std::string > out_iter( std::cout, "\n" );
  85. std::copy( begin, end, out_iter );
  86. This program displays the following:
  87. [pre
  88. 02
  89. 01
  90. 2003
  91. 23
  92. 04
  93. 1999
  94. 13
  95. 11
  96. 1981
  97. ]
  98. The `sub_matches` array instructs the _regex_token_iterator_ to first take the value of the 2nd sub-match, then
  99. the 1st sub-match, and finally the 3rd. Incrementing the iterator again instructs it to use _regex_search_ again
  100. to find the next match. At that point, the process repeats -- the token iterator takes the value of the 2nd
  101. sub-match, then the 1st, et cetera.
  102. [endsect]