// This file is a "Hello, world!" in C++ language by GCC for wandbox.
#include <iostream>
#include <vector>
#include <regex>
#include <string>
std::vector<std::string> split(const std::string& input,
const std::string& regex)
{
std::regex re(regex);
std::sregex_token_iterator first {input.begin(), input.end(), re, -1}, last;
return {first, last};
}
int main()
{
std::string long_str = "this_is_why_we_play";
std::string comma = "_";
auto res_str = split(long_str, comma);
for(auto str: res_str)
std::cout << "str is: " << str << std::endl;
return 0;
}
/*
output:
str is: this
str is: is
str is: why
str is: we
str is: play
*/
C++ 利用正则表达式按特定字符切割字符串
最新推荐文章于 2024-07-10 07:05:23 发布