我正在尝试使用 Boost::regex 将句子拆分为单个单词。但它没有打印最后一个字。有什么想法有什么问题吗?
代码是:
#include <iostream>
#include <boost/regex.hpp>
using namespace std;
using namespace boost;
int main() {
smatch matchResults;
regex whiteChars("(.*?)[\\s]");
string p = "This is a sentence";
for(string::const_iterator sit = p.begin(), sitend = p.end(); sit != sitend;)
{
regex_search(sit, sitend, matchResults, whiteChars);
if(matchResults[1].matched)
cout << matchResults[1] << endl;
sit = matchResults[0].second;
}
return 0;
}
Output:
This
is
a
Expected Output:
This
is
a
sentence