简体   繁体   中英

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"

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()).
}

From your explanation You may use tokenizer function. And put some more logic into it. look at boost::tokenizer

ex:

boost::char_separator<char> sep_1(" ");


std::string msg_copy ("Hello everybody this is a sentense Bla bla 14 .. yes date 04/15/1986 ");
boost::tokenizer< boost::char_separator<char> > tokens(msg_copy, sep_1);
BOOST_FOREACH(std::string t, tokens)
{
        // here you itterate t
}

edit:

You can put as many special characters to separator as you want ex:

boost::char_separator<char> sep_1(" *^&%~/|");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM