【发布时间】:2014-11-30 02:45:48
【问题描述】:
我的字符串包含例如"FirstWord\r\nSecondWord\r\nThird Word\n\r"等等……
我想使用vector <string> 将其拆分为字符串数组,这样我会得到:
FileName[0] == "FirstWord";
FileName[1] == "SecondWord";
FileName[2] == "Third Word";
另外,请注意第三个字符串中的空格。
这是我目前得到的:
string text = Files; // Files var contains the huge string of lines separated by \r\n
vector<string> FileName; // (optionaly) Here I want to store the result without \r\n
regex rx("[^\\s]+\r\n");
sregex_iterator FormatedFileList(text.begin(), text.end(), rx), rxend;
while(FormatedFileList != rxend)
{
FileName.push_back(FormatedFileList->str().c_str());
++FormatedFileList;
}
它有效,但是当涉及到第三个字符串 "Third Word\r\n" 时,它只给我"Word\r\n"。
谁能向我解释正则表达式是如何工作的?我有点困惑。
【问题讨论】:
标签: c++ arrays regex split line-breaks