Skip to content

Instantly share code, notes, and snippets.

@filsinger
Created October 18, 2012 09:06
Show Gist options
  • Save filsinger/3910580 to your computer and use it in GitHub Desktop.
Save filsinger/3910580 to your computer and use it in GitHub Desktop.
C++11 : Split a string using regex
#include <regex>
#include <string>
#include <vector>
std::vector<std::string> Split(const std::string& str, const std::string& regex)
{
return {std::sregex_token_iterator(str.begin(), str.end(), std::regex(regex), -1), std::sregex_token_iterator()};
}
@wyckster
Copy link

It is inappropriate to pass a temporary regex std::regex(regex) to the iterator.

If you are getting:

error C2280: 'std::regex_token_iterator<std::_String_const_iterator<std::_String_val<std::_Simple_types<_Elem>>>,_Elem,std::regex_traits>::regex_token_iterator(_BidIt,_BidIt,const std::basic_regex<_Elem,std::regex_traits> &&,int,std::regex_constants::match_flag_type)': attempting to reference a deleted function

Then please see this Stack Overflow question.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment