partial_matches.qbk 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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:partial_matches Partial Matches]
  8. The [match_flag_type] `match_partial` can be passed to the following algorithms:
  9. [regex_match], [regex_search], and [regex_grep], and used with the
  10. iterator [regex_iterator]. When used it indicates that partial as
  11. well as full matches should be found. A partial match is one that
  12. matched one or more characters at the end of the text input, but
  13. did not match all of the regular expression (although it may have done
  14. so had more input been available). Partial matches are typically used
  15. when either validating data input (checking each character as it is
  16. entered on the keyboard), or when searching texts that are either too long
  17. to load into memory (or even into a memory mapped file), or are of
  18. indeterminate length (for example the source may be a socket or similar).
  19. Partial and full matches can be differentiated as shown in the following
  20. table (the variable M represents an instance of [match_results] as filled in
  21. by [regex_match], [regex_search] or [regex_grep]):
  22. [table
  23. [[ ][Result][M\[0\].matched][M\[0\].first][M\[0\].second]]
  24. [[No match][False][Undefined][Undefined][Undefined]]
  25. [[Partial match][True][False][Start of partial match.][End of partial match (end of text).]]
  26. [[Full match][True][True][Start of full match.][End of full match.]]
  27. ]
  28. Be aware that using partial matches can sometimes result in somewhat
  29. imperfect behavior:
  30. * There are some expressions, such as ".\*abc" that will always produce a partial match. This problem can be reduced by careful construction of the regular expressions used, or by setting flags like match_not_dot_newline so that expressions like .\* can't match past line boundaries.
  31. * Boost.Regex currently prefers leftmost matches to full matches, so for example matching "abc|b" against "ab" produces a partial match against the "ab" rather than a full match against "b". It's more efficient to work this way, but may not be the behavior you want in all situations.
  32. * There are situations where full matches are found even though partial matches are also possible: for example if the partial string terminates with "abc" and the regular expression is "\w+", then a full match is found
  33. even though there may be more alphabetical characters to come. This particular case can be detected by checking if the match found terminates at the end of current input string. However, there are situations where
  34. that is not possible: for example an expression such as "abc.*123" may always have longer matches available since it could conceivably match the entire input string (no matter how long it may be).
  35. The following example tests to see whether the text could be a valid
  36. credit card number, as the user presses a key, the character entered
  37. would be added to the string being built up, and passed to `is_possible_card_number`.
  38. If this returns true then the text could be a valid card number, so the
  39. user interface's OK button would be enabled. If it returns false, then
  40. this is not yet a valid card number, but could be with more input, so
  41. the user interface would disable the OK button. Finally, if the procedure
  42. throws an exception the input could never become a valid number, and the
  43. inputted character must be discarded, and a suitable error indication
  44. displayed to the user.
  45. #include <string>
  46. #include <iostream>
  47. #include <boost/regex.hpp>
  48. boost::regex e("(\\d{3,4})[- ]?(\\d{4})[- ]?(\\d{4})[- ]?(\\d{4})");
  49. bool is_possible_card_number(const std::string& input)
  50. {
  51. //
  52. // return false for partial match, true for full match, or throw for
  53. // impossible match based on what we have so far...
  54. boost::match_results<std::string::const_iterator> what;
  55. if(0 == boost::regex_match(input, what, e, boost::match_default | boost::match_partial))
  56. {
  57. // the input so far could not possibly be valid so reject it:
  58. throw std::runtime_error(
  59. "Invalid data entered - this could not possibly be a valid card number");
  60. }
  61. // OK so far so good, but have we finished?
  62. if(what[0].matched)
  63. {
  64. // excellent, we have a result:
  65. return true;
  66. }
  67. // what we have so far is only a partial match...
  68. return false;
  69. }
  70. In the following example, text input is taken from a stream containing an
  71. unknown amount of text; this example simply counts the number of html tags
  72. encountered in the stream. The text is loaded into a buffer and searched a
  73. part at a time, if a partial match was encountered, then the partial match
  74. gets searched a second time as the start of the next batch of text:
  75. #include <iostream>
  76. #include <fstream>
  77. #include <sstream>
  78. #include <string>
  79. #include <boost/regex.hpp>
  80. // match some kind of html tag:
  81. boost::regex e("<[^>]*>");
  82. // count how many:
  83. unsigned int tags = 0;
  84. void search(std::istream& is)
  85. {
  86. // buffer we'll be searching in:
  87. char buf[4096];
  88. // saved position of end of partial match:
  89. const char* next_pos = buf + sizeof(buf);
  90. // flag to indicate whether there is more input to come:
  91. bool have_more = true;
  92. while(have_more)
  93. {
  94. // how much do we copy forward from last try:
  95. unsigned leftover = (buf + sizeof(buf)) - next_pos;
  96. // and how much is left to fill:
  97. unsigned size = next_pos - buf;
  98. // copy forward whatever we have left:
  99. std::memmove(buf, next_pos, leftover);
  100. // fill the rest from the stream:
  101. is.read(buf + leftover, size);
  102. unsigned read = is.gcount();
  103. // check to see if we've run out of text:
  104. have_more = read == size;
  105. // reset next_pos:
  106. next_pos = buf + sizeof(buf);
  107. // and then iterate:
  108. boost::cregex_iterator a(
  109. buf,
  110. buf + read + leftover,
  111. e,
  112. boost::match_default | boost::match_partial);
  113. boost::cregex_iterator b;
  114. while(a != b)
  115. {
  116. if((*a)[0].matched == false)
  117. {
  118. // Partial match, save position and break:
  119. next_pos = (*a)[0].first;
  120. break;
  121. }
  122. else
  123. {
  124. // full match:
  125. ++tags;
  126. }
  127. // move to next match:
  128. ++a;
  129. }
  130. }
  131. }
  132. [endsect]