使用C ++中的正则表达式拆分包含冒号的字符串并存储在向量中

时间:2016-09-28 09:45:42

标签: c++ regex

我想拆分用冒号分隔的字符串fullString并将结果存储在向量中。正则表达式应该是什么样的?或者我需要一种完全不同的方法吗?

"Input\temp.csv:1:14"

MyVector应该有三个元素

sInpFileName = "Input\temp.csv:1:14";

regex colon(?);
vector<string> MyVector(sregex_token_iterator(sInpFileName.begin(), sInpFileName.end(), colon, 1), sregex_token_iterator());

2 个答案:

答案 0 :(得分:0)

这应该有效:

[live]

std::string sInpFileName = "Input\\temp.csv:1:14";

std::regex re{"([^:]+)"};
std::regex_token_iterator<std::string::iterator> it{sInpFileName.begin(), sInpFileName.end(), re, 1};
decltype(it) end{};
while (it != end) std::cout << *it++ << std::endl;

答案 1 :(得分:0)

这样的事情应该有效。推动变种。

static const boost::regex pat(":");
boost::sregex_token_iterator iter( sInpFileName.begin(), sInpFileName.end(), pat, -1);
boost::sregex_token_iterator end;
std::vector<std::string> MyVector(iter, end);