How to use C ++ Boost regex_iterator ()

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

This is the only use case I have found, but I do not understand the callback. Can someone give me an example of a uage function?


Suppose my input is:

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

I want to receive:

 "Hello" "everybody" "this" "is" "a" "sentense" "bla" "yes" "date" 
+7
c ++ iterator boost regex
source share
2 answers

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

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

approximately equivalent:

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

Assuming that in your case you want to keep all matches in a vector, you should 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()). } 
+8
source share

From your explanation, you can use the tokenizer function. And put one more logic into it. look boost :: tokenizer

Example:

 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 in the delimiter as you want ex:

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

All Articles