C++ - Split string by regex

前端 未结 4 1235
鱼传尺愫
鱼传尺愫 2020-12-05 13:47

I want to split std::string by regex.

I have found some solutions on Stackoverflow, but most of them are splitting string by single space o

4条回答
  •  心在旅途
    2020-12-05 14:43

    string s = "foo bar  baz";
    regex e("\\s+");
    regex_token_iterator i(s.begin(), s.end(), e, -1);
    regex_token_iterator end;
    while (i != end)
       cout << " [" << *i++ << "]";
    

    prints [foo] [bar] [baz]

提交回复
热议问题