我试着用标点符号(.,?,!)把句子分开。我在StackOverflow上找到了一种用一个分隔符分隔字符串的方法,但我还没能找到一种同时根据多个分隔符分隔字符串的方法。以下是我到目前为止拥有的代码:
void chopSentences(std::string new_sentences, std::vector<std::string> sentences) {
size_t pos = 0;
std::string token;
std::string delimiter = ".";
while ((pos = new_sentences.find(delimiter) != std::string::npos)) {
token = new_sentences.substr(0, pos);
sentences.push_back(token);
new_sentences.erase(0, pos + delimiter.length());
}
}有没有关于如何使其超过一个分隔符的想法?
发布于 2021-08-12 18:51:00
如果您使用的是C++11或更高版本,则可以使用std::regex_iterator
std::string const s{"Hello, Johnny! Are you there?"};`
std::regex words_regex("[^[:punct:]\\?]+");
auto words_begin =
std::sregex_iterator(s.begin(), s.end(), words_regex);
auto words_end = std::sregex_iterator();
std::cout << "Found "
<< std::distance(words_begin, words_end)
<< " words:\n";
for (std::sregex_iterator i = words_begin; i != words_end; ++i) {
std::smatch match = *i;
std::string match_str = match.str();
std::cout << match_str << '\n';
}然后打印输出是:
Found 3 words:
Hello
Johnny
Are you there您必须进一步调整正则表达式以删除空格。
https://stackoverflow.com/questions/68761070
复制相似问题