captures.qbk 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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:captures Understanding Marked Sub-Expressions and Captures]
  8. Captures are the iterator ranges that are "captured" by marked
  9. sub-expressions as a regular expression gets matched. Each marked
  10. sub-expression can result in more than one capture, if it is matched
  11. more than once. This document explains how captures and marked
  12. sub-expressions in Boost.Regex are represented and accessed.
  13. [h4 Marked sub-expressions]
  14. Every time a Perl regular expression contains a parenthesis group `()`, it
  15. spits out an extra field, known as a marked sub-expression,
  16. for example the expression:
  17. [pre (\w+)\W+(\w+)]
  18. Has two marked sub-expressions (known as $1 and $2 respectively), in
  19. addition the complete match is known as $&, everything before the
  20. first match as $\`, and everything after the match as $'. So
  21. if the above expression is searched for within `"@abc def--"`, then we obtain:
  22. [table
  23. [[Sub-expression][Text found]]
  24. [[$\`]["@"]]
  25. [[$&]["abc def"]]
  26. [[$1]["abc"]]
  27. [[$2]["def"]]
  28. [[$']["--"]]
  29. ]
  30. In Boost.Regex all these are accessible via the [match_results] class that
  31. gets filled in when calling one of the regular expression matching algorithms
  32. ([regex_search], [regex_match], or [regex_iterator]). So given:
  33. boost::match_results<IteratorType> m;
  34. The Perl and Boost.Regex equivalents are as follows:
  35. [table
  36. [[Perl][Boost.Regex]]
  37. [[$\`][`m.prefix()`]]
  38. [[$&][`m[0]`]]
  39. [[$n][`m[n]`]]
  40. [[$\'][`m.suffix()`]]
  41. ]
  42. In Boost.Regex each sub-expression match is represented by a [sub_match] object,
  43. this is basically just a pair of iterators denoting the start and end
  44. position of the sub-expression match, but there are some additional
  45. operators provided so that objects of type [sub_match] behave a lot like a
  46. `std::basic_string`: for example they are implicitly convertible to a
  47. `basic_string`, they can be compared to a string, added to a string, or
  48. streamed out to an output stream.
  49. [h4 Unmatched Sub-Expressions]
  50. When a regular expression match is found there is no need for all of the
  51. marked sub-expressions to have participated in the match, for example the expression:
  52. [pre (abc)|(def)]
  53. can match either $1 or $2, but never both at the same time. In Boost.Regex
  54. you can determine which sub-expressions matched by accessing the
  55. `sub_match::matched` data member.
  56. [h4 Repeated Captures]
  57. When a marked sub-expression is repeated, then the sub-expression gets
  58. "captured" multiple times, however normally only the final capture is available,
  59. for example if
  60. [pre (?:(\w+)\W+)+]
  61. is matched against
  62. [pre one fine day]
  63. Then $1 will contain the string "day", and all the previous captures will have
  64. been forgotten.
  65. However, Boost.Regex has an experimental feature that allows all the capture
  66. information to be retained - this is accessed either via the
  67. `match_results::captures` member function or the `sub_match::captures` member
  68. function. These functions return a container that contains a sequence of all
  69. the captures obtained during the regular expression matching. The following
  70. example program shows how this information may be used:
  71. #include <boost/regex.hpp>
  72. #include <iostream>
  73. void print_captures(const std::string& regx, const std::string& text)
  74. {
  75. boost::regex e(regx);
  76. boost::smatch what;
  77. std::cout << "Expression: \"" << regx << "\"\n";
  78. std::cout << "Text: \"" << text << "\"\n";
  79. if(boost::regex_match(text, what, e, boost::match_extra))
  80. {
  81. unsigned i, j;
  82. std::cout << "** Match found **\n Sub-Expressions:\n";
  83. for(i = 0; i < what.size(); ++i)
  84. std::cout << " $" << i << " = \"" << what[i] << "\"\n";
  85. std::cout << " Captures:\n";
  86. for(i = 0; i < what.size(); ++i)
  87. {
  88. std::cout << " $" << i << " = {";
  89. for(j = 0; j < what.captures(i).size(); ++j)
  90. {
  91. if(j)
  92. std::cout << ", ";
  93. else
  94. std::cout << " ";
  95. std::cout << "\"" << what.captures(i)[j] << "\"";
  96. }
  97. std::cout << " }\n";
  98. }
  99. }
  100. else
  101. {
  102. std::cout << "** No Match found **\n";
  103. }
  104. }
  105. int main(int , char* [])
  106. {
  107. print_captures("(([[:lower:]]+)|([[:upper:]]+))+", "aBBcccDDDDDeeeeeeee");
  108. print_captures("(.*)bar|(.*)bah", "abcbar");
  109. print_captures("(.*)bar|(.*)bah", "abcbah");
  110. print_captures("^(?:(\\w+)|(?>\\W+))*$",
  111. "now is the time for all good men to come to the aid of the party");
  112. return 0;
  113. }
  114. Which produces the following output:
  115. [pre
  116. Expression: "((\[\[:lower:\]\]+)|(\[\[:upper:\]\]+))+"
  117. Text: "aBBcccDDDDDeeeeeeee"
  118. '''**''' Match found '''**'''
  119. Sub-Expressions:
  120. $0 = "aBBcccDDDDDeeeeeeee"
  121. $1 = "eeeeeeee"
  122. $2 = "eeeeeeee"
  123. $3 = "DDDDD"
  124. Captures:
  125. $0 = { "aBBcccDDDDDeeeeeeee" }
  126. $1 = { "a", "BB", "ccc", "DDDDD", "eeeeeeee" }
  127. $2 = { "a", "ccc", "eeeeeeee" }
  128. $3 = { "BB", "DDDDD" }
  129. Expression: "(.'''*''')bar|(.'''*''')bah"
  130. Text: "abcbar"
  131. '''**''' Match found '''**'''
  132. Sub-Expressions:
  133. $0 = "abcbar"
  134. $1 = "abc"
  135. $2 = ""
  136. Captures:
  137. $0 = { "abcbar" }
  138. $1 = { "abc" }
  139. $2 = { }
  140. Expression: "(.'''*''')bar|(.'''*''')bah"
  141. Text: "abcbah"
  142. '''**''' Match found '''**'''
  143. Sub-Expressions:
  144. $0 = "abcbah"
  145. $1 = ""
  146. $2 = "abc"
  147. Captures:
  148. $0 = { "abcbah" }
  149. $1 = { }
  150. $2 = { "abc" }
  151. Expression: "^(?:(\w+)|(?>\W+))'''*$'''"
  152. Text: "now is the time for all good men to come to the aid of the party"
  153. '''**''' Match found '''**'''
  154. Sub-Expressions:
  155. $0 = "now is the time for all good men to come to the aid of the party"
  156. $1 = "party"
  157. Captures:
  158. $0 = { "now is the time for all good men to come to the aid of the party" }
  159. $1 = { "now", "is", "the", "time", "for", "all", "good", "men", "to",
  160. "come", "to", "the", "aid", "of", "the", "party" }
  161. ]
  162. Unfortunately enabling this feature has an impact on performance
  163. (even if you don't use it), and a much bigger impact if you do use it,
  164. therefore to use this feature you need to:
  165. * Define BOOST_REGEX_MATCH_EXTRA for all translation units including the library source (the best way to do this is to uncomment this define in boost/regex/user.hpp and then rebuild everything.
  166. * Pass the match_extra flag to the particular algorithms where you actually need the captures information (regex_search, regex_match, or regex_iterator).
  167. [endsect]