regex_search.qbk 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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_search regex_search]
  8. #include <boost/regex.hpp>
  9. The algorithm [regex_search] will search a range denoted by a pair of
  10. bidirectional-iterators for a given regular expression. The algorithm
  11. uses various heuristics to reduce the search time by only checking
  12. for a match if a match could conceivably start at that position. The
  13. algorithm is defined as follows:
  14. template <class BidirectionalIterator,
  15. class Allocator, class charT, class traits>
  16. bool regex_search(BidirectionalIterator first, BidirectionalIterator last,
  17. match_results<BidirectionalIterator, Allocator>& m,
  18. const basic_regex<charT, traits>& e,
  19. match_flag_type flags = match_default);
  20. template <class ST, class SA,
  21. class Allocator, class charT, class traits>
  22. bool regex_search(const basic_string<charT, ST, SA>& s,
  23. match_results<
  24. typename basic_string<charT, ST,SA>::const_iterator,
  25. Allocator>& m,
  26. const basic_regex<charT, traits>& e,
  27. match_flag_type flags = match_default);
  28. template<class charT, class Allocator, class traits>
  29. bool regex_search(const charT* str,
  30. match_results<const charT*, Allocator>& m,
  31. const basic_regex<charT, traits>& e,
  32. match_flag_type flags = match_default);
  33. template <class BidirectionalIterator, class charT, class traits>
  34. bool regex_search(BidirectionalIterator first, BidirectionalIterator last,
  35. const basic_regex<charT, traits>& e,
  36. match_flag_type flags = match_default);
  37. template <class charT, class traits>
  38. bool regex_search(const charT* str,
  39. const basic_regex<charT, traits>& e,
  40. match_flag_type flags = match_default);
  41. template<class ST, class SA, class charT, class traits>
  42. bool regex_search(const basic_string<charT, ST, SA>& s,
  43. const basic_regex<charT, traits>& e,
  44. match_flag_type flags = match_default);
  45. [h4 Description]
  46. template <class BidirectionalIterator, class Allocator, class charT, class traits>
  47. bool regex_search(BidirectionalIterator first, BidirectionalIterator last,
  48. match_results<BidirectionalIterator, Allocator>& m,
  49. const basic_regex<charT, traits>& e,
  50. match_flag_type flags = match_default);
  51. [*Requires]: Type BidirectionalIterator meets the requirements of a Bidirectional Iterator (24.1.4).
  52. [*Effects]: Determines whether there is some sub-sequence within \[first,last)
  53. that matches the regular expression /e/, parameter /flags/ is used to control
  54. how the expression is matched against the character sequence. Returns
  55. true if such a sequence exists, false otherwise.
  56. [*Throws]: `std::runtime_error` if the complexity of matching the expression
  57. against an N character string begins to exceed O(N[super 2]), or if the
  58. program runs out of stack space while matching the expression (if Boost.Regex is
  59. configured in recursive mode), or if the matcher exhausts its permitted
  60. memory allocation (if Boost.Regex is configured in non-recursive mode).
  61. [*Postconditions]: If the function returns false, then the effect on
  62. parameter /m/ is undefined, otherwise the effects on parameter /m/
  63. are given in the table:
  64. [table
  65. [[Element][Value]]
  66. [[`m.size()`][`1 + e.mark_count()`]]
  67. [[`m.empty()`][`false`]]
  68. [[`m.prefix().first`][`first`]]
  69. [[`m.prefix().last`][`m[0].first`]]
  70. [[`m.prefix().matched`][`m.prefix().first != m.prefix().second`]]
  71. [[`m.suffix().first`][`m[0].second`]]
  72. [[`m.suffix().last`][`last`]]
  73. [[`m.suffix().matched`][`m.suffix().first != m.suffix().second`]]
  74. [[`m[0].first`][The start of the sequence of characters that matched the regular expression]]
  75. [[`m[0].second`][The end of the sequence of characters that matched the regular expression]]
  76. [[`m[0].matched`][true if a full match was found, and false if it was a partial match (found as a result of the match_partial flag being set).]]
  77. [[`m[n].first`][For all integers `n < m.size()`, the start of the sequence that
  78. matched sub-expression /n/. Alternatively, if sub-expression /n/ did not
  79. participate in the match, then last.]]
  80. [[`m[n].second`][For all integers `n < m.size()`, the end of the sequence that
  81. matched sub-expression /n/. Alternatively, if sub-expression /n/ did not
  82. participate in the match, then `last`.]]
  83. [[`m[n].matched`][For all integers `n < m.size()`, true if sub-expression /n/
  84. participated in the match, false otherwise.]]
  85. ]
  86. template <class charT, class Allocator, class traits>
  87. bool regex_search(const charT* str, match_results<const charT*, Allocator>& m,
  88. const basic_regex<charT, traits>& e,
  89. match_flag_type flags = match_default);
  90. [*Effects]: Returns the result of `regex_search(str, str + char_traits<charT>::length(str), m, e, flags)`.
  91. template <class ST, class SA, class Allocator, class charT,
  92. class traits>
  93. bool regex_search(const basic_string<charT, ST, SA>& s,
  94. match_results<typename basic_string<charT, ST, SA>::const_iterator, Allocator>& m,
  95. const basic_regex<charT, traits>& e,
  96. match_flag_type flags = match_default);
  97. [*Effects]: Returns the result of `regex_search(s.begin(), s.end(), m, e, flags)`.
  98. template <class iterator, class charT, class traits>
  99. bool regex_search(iterator first, iterator last,
  100. const basic_regex<charT, traits>& e,
  101. match_flag_type flags = match_default);
  102. [*Effects]: Behaves "as if" by constructing an instance of
  103. `match_results<BidirectionalIterator> what`, and then returning the result of
  104. `regex_search(first, last, what, e, flags)`.
  105. template <class charT, class traits>
  106. bool regex_search(const charT* str
  107. const basic_regex<charT, traits>& e,
  108. match_flag_type flags = match_default);
  109. [*Effects]: Returns the result of `regex_search(str, str + char_traits<charT>::length(str), e, flags)`.
  110. template <class ST, class SA, class charT, class traits>
  111. bool regex_search(const basic_string<charT, ST, SA>& s,
  112. const basic_regex<charT, traits>& e,
  113. match_flag_type flags = match_default);
  114. [*Effects]: Returns the result of `regex_search(s.begin(), s.end(), e, flags)`.
  115. [h4 Examples]
  116. The following example, takes the contents of a file in the form of a string,
  117. and searches for all the C++ class declarations in the file. The code will
  118. work regardless of the way that `std::string` is implemented, for example it
  119. could easily be modified to work with the SGI rope class, which uses a
  120. non-contiguous storage strategy.
  121. #include <string>
  122. #include <map>
  123. #include <boost/regex.hpp>
  124. // purpose:
  125. // takes the contents of a file in the form of a string
  126. // and searches for all the C++ class definitions, storing
  127. // their locations in a map of strings/int's
  128. typedef std::map<std::string, int, std::less<std::string> > map_type;
  129. boost::regex expression(
  130. "^(template[[:space:]]*<[^;:{]+>[[:space:]]*)?"
  131. "(class|struct)[[:space:]]*"
  132. "(\\<\\w+\\>([[:blank:]]*\\([^)]*\\))?"
  133. "[[:space:]]*)*(\\<\\w*\\>)[[:space:]]*"
  134. "(<[^;:{]+>[[:space:]]*)?(\\{|:[^;\\{()]*\\{)");
  135. void IndexClasses(map_type& m, const std::string& file)
  136. {
  137. std::string::const_iterator start, end;
  138. start = file.begin();
  139. end = file.end();
  140. boost::match_results<std::string::const_iterator> what;
  141. boost::match_flag_type flags = boost::match_default;
  142. while(regex_search(start, end, what, expression, flags))
  143. {
  144. // what[0] contains the whole string
  145. // what[5] contains the class name.
  146. // what[6] contains the template specialisation if any.
  147. // add class name and position to map:
  148. m[std::string(what[5].first, what[5].second)
  149. + std::string(what[6].first, what[6].second)]
  150. = what[5].first - file.begin();
  151. // update search position:
  152. start = what[0].second;
  153. // update flags:
  154. flags |= boost::match_prev_avail;
  155. flags |= boost::match_not_bob;
  156. }
  157. }
  158. [endsect]