Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use C++ Boost's regex_iterator()

I am using Boost to match substrings in a string. Io iterate over the results, I need to use regex_iterator().

That is the only usage example I have found, but I do not understand the callback. Could someone give me an example uage of the function?


Let us assume that my input text is:

"Hello everybody this is a sentense
Bla bla 14 .. yes 
date 04/15/1986 
"

I want to get:

"Hello" "everybody" "this" "is" "a" "sentense" "bla" "yes" "date"
like image 877
Youssef Avatar asked Apr 07 '10 14:04

Youssef


1 Answers

If the only part of the example you don't understand is the callback, consider that:

std::for_each(m1, m2, &regex_callback);

is roughly equivalent to:

for (; m1 != m2; ++m1){
    class_index[(*m1)[5].str() + (*m1)[6].str()] = (*m1).position(5);
}

Assuming that, in your case, you want to store all the matches in a vector, you would write something like:

//Warning, untested:
boost::sregex_iterator m1(text.begin(), text.end(), expression);
boost::sregex_iterator m2;
std::vector<std::string> tokens;
for (; m1 != m2; ++m1){
    tokens.push_back(m1->str()).
}
like image 117
Éric Malenfant Avatar answered Sep 21 '22 23:09

Éric Malenfant