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).
9 [section:captures Understanding Marked Sub-Expressions and Captures]
11 Captures are the iterator ranges that are "captured" by marked
12 sub-expressions as a regular expression gets matched. Each marked
13 sub-expression can result in more than one capture, if it is matched
14 more than once. This document explains how captures and marked
15 sub-expressions in Boost.Regex are represented and accessed.
17 [h4 Marked sub-expressions]
19 Every time a Perl regular expression contains a parenthesis group `()`, it
20 spits out an extra field, known as a marked sub-expression,
21 for example the expression:
25 Has two marked sub-expressions (known as $1 and $2 respectively), in
26 addition the complete match is known as $&, everything before the
27 first match as $\`, and everything after the match as $'. So
28 if the above expression is searched for within `"@abc def--"`, then we obtain:
31 [[Sub-expression][Text found]]
39 In Boost.Regex all these are accessible via the [match_results] class that
40 gets filled in when calling one of the regular expression matching algorithms
41 ([regex_search], [regex_match], or [regex_iterator]). So given:
43 boost::match_results<IteratorType> m;
45 The Perl and Boost.Regex equivalents are as follows:
55 In Boost.Regex each sub-expression match is represented by a [sub_match] object,
56 this is basically just a pair of iterators denoting the start and end
57 position of the sub-expression match, but there are some additional
58 operators provided so that objects of type [sub_match] behave a lot like a
59 `std::basic_string`: for example they are implicitly convertible to a
60 `basic_string`, they can be compared to a string, added to a string, or
61 streamed out to an output stream.
63 [h4 Unmatched Sub-Expressions]
65 When a regular expression match is found there is no need for all of the
66 marked sub-expressions to have participated in the match, for example the expression:
70 can match either $1 or $2, but never both at the same time. In Boost.Regex
71 you can determine which sub-expressions matched by accessing the
72 `sub_match::matched` data member.
74 [h4 Repeated Captures]
76 When a marked sub-expression is repeated, then the sub-expression gets
77 "captured" multiple times, however normally only the final capture is available,
86 Then $1 will contain the string "day", and all the previous captures will have
89 However, Boost.Regex has an experimental feature that allows all the capture
90 information to be retained - this is accessed either via the
91 `match_results::captures` member function or the `sub_match::captures` member
92 function. These functions return a container that contains a sequence of all
93 the captures obtained during the regular expression matching. The following
94 example program shows how this information may be used:
96 #include <boost/regex.hpp>
99 void print_captures(const std::string& regx, const std::string& text)
101 boost::regex e(regx);
103 std::cout << "Expression: \"" << regx << "\"\n";
104 std::cout << "Text: \"" << text << "\"\n";
105 if(boost::regex_match(text, what, e, boost::match_extra))
108 std::cout << "** Match found **\n Sub-Expressions:\n";
109 for(i = 0; i < what.size(); ++i)
110 std::cout << " $" << i << " = \"" << what[i] << "\"\n";
111 std::cout << " Captures:\n";
112 for(i = 0; i < what.size(); ++i)
114 std::cout << " $" << i << " = {";
115 for(j = 0; j < what.captures(i).size(); ++j)
121 std::cout << "\"" << what.captures(i)[j] << "\"";
128 std::cout << "** No Match found **\n";
132 int main(int , char* [])
134 print_captures("(([[:lower:]]+)|([[:upper:]]+))+", "aBBcccDDDDDeeeeeeee");
135 print_captures("(.*)bar|(.*)bah", "abcbar");
136 print_captures("(.*)bar|(.*)bah", "abcbah");
137 print_captures("^(?:(\\w+)|(?>\\W+))*$",
138 "now is the time for all good men to come to the aid of the party");
142 Which produces the following output:
145 Expression: "((\[\[:lower:\]\]+)|(\[\[:upper:\]\]+))+"
146 Text: "aBBcccDDDDDeeeeeeee"
147 '''**''' Match found '''**'''
149 $0 = "aBBcccDDDDDeeeeeeee"
154 $0 = { "aBBcccDDDDDeeeeeeee" }
155 $1 = { "a", "BB", "ccc", "DDDDD", "eeeeeeee" }
156 $2 = { "a", "ccc", "eeeeeeee" }
157 $3 = { "BB", "DDDDD" }
158 Expression: "(.'''*''')bar|(.'''*''')bah"
160 '''**''' Match found '''**'''
169 Expression: "(.'''*''')bar|(.'''*''')bah"
171 '''**''' Match found '''**'''
180 Expression: "^(?:(\w+)|(?>\W+))'''*$'''"
181 Text: "now is the time for all good men to come to the aid of the party"
182 '''**''' Match found '''**'''
184 $0 = "now is the time for all good men to come to the aid of the party"
187 $0 = { "now is the time for all good men to come to the aid of the party" }
188 $1 = { "now", "is", "the", "time", "for", "all", "good", "men", "to",
189 "come", "to", "the", "aid", "of", "the", "party" }
192 Unfortunately enabling this feature has an impact on performance
193 (even if you don't use it), and a much bigger impact if you do use it,
194 therefore to use this feature you need to:
196 * 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.
197 * Pass the match_extra flag to the particular algorithms where you actually need the captures information (regex_search, regex_match, or regex_iterator).