0
使用std C++,我想分割一個由逗號分隔的字符串,但忽略單引號包圍的字符串中的逗號。例如:包含逗號與逗號分隔符的分割字符串
1, '2,3',4,5, '6,7',8
當分裂成爲
1
'2,3'
4
5
'6,7'
8
我認爲這可能與正則表達式來最好的處理,但我不知道如何構建模式。任何沒有正則表達式的解決方案也是受歡迎的。謝謝。
使用std C++,我想分割一個由逗號分隔的字符串,但忽略單引號包圍的字符串中的逗號。例如:包含逗號與逗號分隔符的分割字符串
1, '2,3',4,5, '6,7',8
當分裂成爲
1
'2,3'
4
5
'6,7'
8
我認爲這可能與正則表達式來最好的處理,但我不知道如何構建模式。任何沒有正則表達式的解決方案也是受歡迎的。謝謝。
我不知道C++的語法是什麼,但這裏的一些僞代碼:
vector<string> split(const string& value)
{
bool is_escaped = false;
vector<char> current;
vector<string> result;
for (char c : value)
{
if (c == '\'')
{
is_escaped = !is_escaped;
}
if (c == ',' && !is_escaped)
{
result.push_back(string(current.begin(), current.end());
current.clear();
}
else
{
current.push_back(c);
}
}
result.push_back(string(current.begin(), current.end());
return result;
}
顯然,你需要調整它是有效的C++,但它應該做的伎倆。
只需幾次更改就可以完成工作。我想我需要更多地熟悉標準庫。謝謝! – user638473