如何在c++的字符串中找到一个完整的单词(不是它的一部分)

How do I find a complete word (not part of it) in a string in C++

本文关键字:一个 一部分 单词 c++ 字符串      更新时间:2023-10-16

在c++代码中,我试图在句子中搜索一个单词,但它一直在做部分搜索。我想让它只搜索整个单词,而不是部分单词,有什么帮助吗?

size_t kk;
string word="spo";
string sentence="seven spoons";
kk=sentence.find(word);
if (kk !=string::npos)
cout << "something" << endl;

听起来你想要的是由正则表达式中的词边界或词字符的概念来处理的。

这是一个只返回完全匹配的程序。也就是说,它只会返回与您正在搜索的单词完全匹配的单词。如果sentence中的某些单词将您的目标单词作为严格子字符串,那么它将不会返回。

#include <regex>
#include <string>
#include <iostream>
int main() {
  std::string word = "spo"; // spo is a word?
  std::string sentence = "seven spoons";
  std::regex r("\b" + word + "\b"); // the pattern b matches a word boundary
  std::smatch m;
  if (std::regex_search(sentence, m, r)) { // this won't find anything because 'spoons' is not the word you're searching for
    std::cout << "match 1: " << m.str() << 'n';
  }
  sentence = "what does the word 'spo' mean?";    
  if (std::regex_search(sentence, m, r)) { // this does find the word 'spo'
    std::cout << "match 2: " << m.str() << 'n';
  }
}

或者你的意思是你想找到与你正在搜索的部分单词匹配的任何单词。Regex也可以这样做:

  std::string partial_word = "spo";
  std::regex r("\w*" + partial_word + "\w*"); // the pattern w matches a word character

这产生:

match 1: spoons
match 2: spo

这里有一堆选项:

a)搜索[space]WORD[space]而不仅仅是WORD

string word="spo";
string sentence="seven spoons";
kk=sentence.find(" "+word+" ");

请注意,如果你的单词被换行符或其他空格分隔,这将不起作用。

b)将字符串拆分为单词,将它们存储在vector中,并通过使用std::find检查所需的单词是否在向量中的某个地方。

stringstream parser(sentence);
istream_iterator<string> start(parser);
istream_iterator<string> end;
vector<string> words(start, end);
if(find(words.begin(), words.end(), word)!=words.end()) cout<<"found!";

如果你要经常搜索单词,这可能是最好的选择,因为你可以将向量存储在某个地方以备将来参考,所以你不必拆分它。另外,如果你想让它工作,确保#include <algorithm>#include <vector>

c)查找单词并检查isspace(string[position-1]) && isspace(string[position+wordLength])

string word="spo";
string sentence="seven spoons";
kk=sentence.find(" "+word+" ");
if(kk!=string::npos){
    if((kk==0 || isspace(sentence[kk-1])) && (kk+word.length()==sentence.length() || isspace(kk+word.length()+1)))
       cout << "found!";
}

像这样:

std::size_t kk;
std::string word="spoo";
std::string sentence="seven spoons tables";
std::stringstream ss(sentence) ;
std::istream_iterator<std::string> f ;
auto it =std::find_if(  std::istream_iterator<std::string> (ss),
                        f,
                        [=](const std::string& str){
                        return str == word;
                        }
 );
if(it != f )
 std::cout << "Success" <<std::endl;

看到

我认为最好的方法是使用空格和标点字符作为分隔符拆分字符串,然后在结果上使用std::find

#include <boost/algorithm/string.hpp>
#include <vector>
#include <string>
#include <algorithm>
int main()
{
    std::string word="spo";
    std::string sentence="seven spoons";
    std::vector<std::string> words;
    boost::split(words, sentence, boost::is_any_of("nt .,!?"()"));
    auto match = std::find(begin(words), end(words), word);
    if (match != end(words))
    {
        // Found it!
    }
    else
    {
        // Not there.
    }
 }
string word="spo";  
string sentence="seven spoons";  
string::size_type nIndex = sentence.find( word, 0 );  
if( nIndex != string::npos )  
{  
if ((nIndex + word.length() + 1) == sentence.length())  
{  
    cout << "Found" << endl;  
}  
else  
{  
string::size_type nSpace = sentence.find( " ", nIndex );  
if (nSpace == (nIndex + word.length()))  
{  
cout << "Found" << endl;  
}  
}  
}  
else  
{  
cout << "No Match" << endl;  
}  

这似乎起作用了。

#include <string>
/*find word in sentence and return the index of first occurrence*/
int find_whole(string sentence,string word){
    size_t pos=sentence.find(word);
    size_t offset=pos+sentence.size()+1;
    if((pos!=string::npos) && (sentence.substr(pos,offset)==word))
        return pos;
    return string::npos;
}