regex_match.qbk 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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_match regex_match]
  8. #include <boost/regex.hpp>
  9. The algorithm [regex_match] determines whether a given regular expression
  10. matches [*all] of a given character sequence denoted by a pair of
  11. bidirectional-iterators, the algorithm is defined as follows,
  12. the main use of this function is data input validation.
  13. [important Note that the result is true only if the expression matches the
  14. *whole* of the input sequence. If you want to search for an
  15. expression somewhere within the sequence then use [regex_search]. If you
  16. want to match a prefix of the character string then use [regex_search]
  17. with the flag match_continuous set.]
  18. template <class BidirectionalIterator, class Allocator, class charT, class traits>
  19. bool regex_match(BidirectionalIterator first, BidirectionalIterator last,
  20. match_results<BidirectionalIterator, Allocator>& m,
  21. const basic_regex <charT, traits>& e,
  22. match_flag_type flags = match_default);
  23. template <class BidirectionalIterator, class charT, class traits>
  24. bool regex_match(BidirectionalIterator first, BidirectionalIterator last,
  25. const basic_regex <charT, traits>& e,
  26. match_flag_type flags = match_default);
  27. template <class charT, class Allocator, class traits>
  28. bool regex_match(const charT* str, match_results<const charT*, Allocator>& m,
  29. const basic_regex <charT, traits>& e,
  30. match_flag_type flags = match_default);
  31. template <class ST, class SA, class Allocator, class charT, class traits>
  32. bool regex_match(const basic_string<charT, ST, SA>& s,
  33. match_results<typename basic_string<charT, ST, SA>::const_iterator, Allocator>& m,
  34. const basic_regex <charT, traits>& e,
  35. match_flag_type flags = match_default);
  36. template <class charT, class traits>
  37. bool regex_match(const charT* str,
  38. const basic_regex <charT, traits>& e,
  39. match_flag_type flags = match_default);
  40. template <class ST, class SA, class charT, class traits>
  41. bool regex_match(const basic_string<charT, ST, SA>& s,
  42. const basic_regex <charT, traits>& e,
  43. match_flag_type flags = match_default);
  44. [h4 Description]
  45. template <class BidirectionalIterator, class Allocator, class charT, class traits>
  46. bool regex_match(BidirectionalIterator first, BidirectionalIterator last,
  47. match_results<BidirectionalIterator, Allocator>& m,
  48. const basic_regex <charT, traits>& e,
  49. match_flag_type flags = match_default);
  50. [*Requires]: Type BidirectionalIterator meets the requirements of a
  51. Bidirectional Iterator (24.1.4).
  52. [*Effects]: Determines whether there is an exact match between the regular expression /e/,
  53. and all of the character sequence \[first, last), parameter /flags/
  54. (see [match_flag_type]) is used to
  55. control how the expression is matched against the character sequence.
  56. Returns true if such a match exists, false otherwise.
  57. [*Throws]: `std::runtime_error` if the complexity of matching the expression
  58. against an N character string begins to exceed O(N[super 2]), or if the
  59. program runs out of stack space while matching the expression (if Boost.Regex is
  60. configured in recursive mode), or if the matcher exhausts its permitted
  61. memory allocation (if Boost.Regex is configured in non-recursive mode).
  62. [*Postconditions]: If the function returns false, then the effect on
  63. parameter /m/ is undefined, otherwise the effects on parameter /m/ are
  64. given in the table:
  65. [table
  66. [[Element][Value]]
  67. [[`m.size()`][`1 + e.mark_count()`]]
  68. [[`m.empty()`][`false`]]
  69. [[`m.prefix().first`][`first`]]
  70. [[`m.prefix().last`][`first`]]
  71. [[`m.prefix().matched`][`false`]]
  72. [[`m.suffix().first`][`last`]]
  73. [[`m.suffix().last`][`last`]]
  74. [[`m.suffix().matched`][`false`]]
  75. [[`m[0].first`][`first`]]
  76. [[`m[0].second`][`last`]]
  77. [[`m[0].matched`][true if a full match was found, and false if it was a
  78. partial match (found as a result of the match_partial flag being set).]]
  79. [[`m[n].first`][For all integers `n < m.size()`, the start of the sequence that
  80. matched sub-expression /n/. Alternatively, if sub-expression /n/ did not
  81. participate in the match, then `last`.]]
  82. [[`m[n].second`][For all integers `n < m.size()`, the end of the sequence that
  83. matched sub-expression /n/. Alternatively, if sub-expression /n/ did not
  84. participate in the match, then `last`.]]
  85. [[`m[n].matched`][For all integers `n < m.size()`, true if sub-expression /n/
  86. participated in the match, false otherwise.]]
  87. ]
  88. template <class BidirectionalIterator, class charT, class traits>
  89. bool regex_match(BidirectionalIterator first, BidirectionalIterator last,
  90. const basic_regex <charT, traits>& e,
  91. match_flag_type flags = match_default);
  92. [*Effects]: Behaves "as if" by constructing an instance of
  93. `match_results<BidirectionalIterator> what`, and then returning the result of
  94. `regex_match(first, last, what, e, flags)`.
  95. template <class charT, class Allocator, class traits>
  96. bool regex_match(const charT* str, match_results<const charT*, Allocator>& m,
  97. const basic_regex <charT, traits>& e,
  98. match_flag_type flags = match_default);
  99. [*Effects]: Returns the result of `regex_match(str, str + char_traits<charT>::length(str), m, e, flags)`.
  100. template <class ST, class SA, class Allocator,
  101. class charT, class traits>
  102. bool regex_match(const basic_string<charT, ST, SA>& s,
  103. match_results<typename basic_string<charT, ST, SA>::const_iterator, Allocator>& m,
  104. const basic_regex <charT, traits>& e,
  105. match_flag_type flags = match_default);
  106. [*Effects]: Returns the result of `regex_match(s.begin(), s.end(), m, e, flags)`.
  107. template <class charT, class traits>
  108. bool regex_match(const charT* str,
  109. const basic_regex <charT, traits>& e,
  110. match_flag_type flags = match_default);
  111. [*Effects]: Returns the result of `regex_match(str, str + char_traits<charT>::length(str), e, flags)`.
  112. template <class ST, class SA, class charT, class traits>
  113. bool regex_match(const basic_string<charT, ST, SA>& s,
  114. const basic_regex <charT, traits>& e,
  115. match_flag_type flags = match_default);
  116. [*Effects]: Returns the result of `regex_match(s.begin(), s.end(), e, flags)`.
  117. [h4 Examples]
  118. The following example processes an ftp response:
  119. #include <stdlib.h>
  120. #include <boost/regex.hpp>
  121. #include <string>
  122. #include <iostream>
  123. using namespace boost;
  124. regex expression("([0-9]+)(\\-| |$)(.*)");
  125. // process_ftp:
  126. // on success returns the ftp response code, and fills
  127. // msg with the ftp response message.
  128. int process_ftp(const char* response, std::string* msg)
  129. {
  130. cmatch what;
  131. if(regex_match(response, what, expression))
  132. {
  133. // what[0] contains the whole string
  134. // what[1] contains the response code
  135. // what[2] contains the separator character
  136. // what[3] contains the text message.
  137. if(msg)
  138. msg->assign(what[3].first, what[3].second);
  139. return std::atoi(what[1].first);
  140. }
  141. // failure did not match
  142. if(msg)
  143. msg->erase();
  144. return -1;
  145. }
  146. [endsect]