Online Book Reader

Home Category

Professional C__ - Marc Gregoire [261]

By Root 1165 0
a source string, extracts every word from that string, and prints it between quotes. The regular expression in this case is [\\w]+, which searches for one or more word-letters. This example uses std::string as source, so it uses sregex_iterator for the iterators. A standard iterator loop is used, but in this case, the end iterator is done slightly differently from the end iterators of ordinary STL containers. Normally, you specify an end iterator for a particular container, but for regex_iterator, there is only one “end” value. You can get this end iterator by simply declaring a regex_iterator type using the default constructor; it will implicitly be initialized to the end value.

The for loop creates a start iterator called it, which accepts a begin and end iterator into the source string together with the regular expression. The loop body will be called for every match found, which is every word in this example. The sregex_iterator iterates over all the matches. By dereferencing a sregex_iterator, you get a smatch object. Accessing the first element of this smatch object, [0], gives you the matched sub-string:

regex reg("[\w]+");

while (true) {

cout << "Enter a string to split (q=quit): ";

string str;

if (!getline(cin, str) || str == "q")

break;

const sregex_iterator end;

for (sregex_iterator it(str.begin(), str.end(), reg); it != end; ++it) {

cout << "\"" << (*it)[0] << "\"" << endl;

}

}

Code snippet from RegularExpressions\regex_iterator.cpp

The output of this program can look as follows:

Enter a string to split (q=quit): This, is a test.

"This"

"is"

"a"

"test"

As this example demonstrates, even simple regular expressions can do some powerful string manipulation.

regex_token_iterator

The previous section described regex_iterator which iterates through every matched pattern. In each iteration of the loop you get a match_results object, which you can use to extract sub-expressions for that match captured by capture groups.

A regex_token_iterator can be used to automatically iterate over all or selected capture groups across all matched patterns. It has four constructors. The first creates an iterator that only iterates over capture groups with given index submatch. The second iterates over all capture groups with an index that appears in the submatches vector. The third iterates over all capture groups with an index that appears in the submatches initializer list and the fourth iterates over all capture groups with an index that appears in the submatches array.

regex_token_iterator(BidirectionalIterator a,

BidirectionalIterator b,

const regex_type& re,

int submatch = 0,

regex_constants::match_flag_type m =

regex_constants::match_default);

regex_token_iterator(BidirectionalIterator a,

BidirectionalIterator b,

const regex_type& re,

const std::vector& submatches,

regex_constants::match_flag_type m =

regex_constants::match_default);

regex_token_iterator(BidirectionalIterator a,

BidirectionalIterator b,

const regex_type& re,

initializer_list submatches,

regex_constants::match_flag_type m =

regex_constants::match_default);

template

regex_token_iterator(BidirectionalIterator a,

BidirectionalIterator b,

const regex_type& re,

const int (&submatches)[N],

regex_constants::match_flag_type m =

regex_constants::match_default);

When you use the first constructor and use the default value of 0 for submatch, you get an iterator that iterates over all capture groups with index 0, which are the sub-strings matching the full regular expression.

regex_token_iterator Examples

The previous regex_iterator example can be rewritten by using a regex_token_iterator as follows. Since the token iterator will automatically iterate over all capture groups with index 0, you use *iter in the loop body instead of (*iter)[0]. The output of this code is exactly the same as the output generated by the regex_iterator example:

regex reg("[\w]+");

while (true) {

cout << "Enter a string to split (q=quit): ";

string str;

if (!getline(cin, str) || str == "q")

break;

const

Return Main Page Previous Page Next Page

®Online Book Reader