I am using VC++ 10 in a project. Being new to C/C++ I just Googled, it appears that in standard C++ doesnt have regex? VC++ 10 seems to have regex. However, how do I do a regex split? Do I need boost just for that?
Searching the web, I found that many recommend Boost for many things, tokenizing/splitting string, parsing (PEG), and now even regex (though this should be build in …). Can I conclude boost is a must have? Its 180MB for just trivial things, supported naively in many languages?
C++11 standard has
std::regex. It also included inTR1 for Visual Studio 2010. Actually TR1 is available since VS2008, it’s hidden understd::tr1namespace. So you don’t need Boost.Regex for VS2008 or later.Splitting can be performed using
regex_token_iterator:if you need to get also the separator itself, you could obtain it from
sub_matchobject pointed bytoken, it is pair containing start and end iterators of token.This is sample for case when you have single char separator. If separator itself can be any substring you need to do some more complex iterator work and possible store previous token submatch object.
Or you can use regex groups and place separators in first group and the real token in second:
Not sure it is 100% correct, but just to illustrate the idea.