正規表示式令牌迭代器示例

一個 std::regex_token_iterator 提供了一個巨大的工具提取逗號分隔值檔案的元素 。除了迭代的優點之外,這個迭代器還能夠捕獲其他方法困難的轉義逗號:

const auto input = "please split,this,csv, ,line,\\,\n"s;
const regex re{ "((?:[^\\\\,]|\\\\.)+)(?:,|$)" };
const vector<string> m_vecFields{ sregex_token_iterator(cbegin(input), cend(input), re, 1), sregex_token_iterator() };

cout << input << endl;

copy(cbegin(m_vecFields), cend(m_vecFields), ostream_iterator<string>(cout, "\n"));

例項

值得注意的是,使用正規表示式迭代器,regex 引數必須是 L 值。 R 值不起作用