简体   繁体   English

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

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

In a C++ code, I'm trying to search for a word in a sentence but it keeps doing partial search. 在C ++代码中,我试图在一个句子中搜索一个单词,但它仍在进行部分搜索。 I want it to search only for the complete word not parts of it too, any help? 我希望它只搜索完整的单词,而不是它的任何部分,任何帮助?

size_t kk;
string word="spo";
string sentence="seven spoons";

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

It sounds like what you want is handled by the concept of word boundaries or word characters in regular expressions. 这听起来像你想要的是由正则表达式中的单词边界或单词字符的概念处理。

Here's a program that will return only a complete match. 这是一个只返回完整匹配的程序。 That is, it will only return a word if that word completely matches the exact word you're searching for. 也就是说,如果该单词与您正在搜索的确切单词完全匹配,它将只返回一个单词。 If some word in sentence has your target word as a strict substring then it will not be returned. 如果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';
  }
}

Or alternatively maybe you mean you want to find any word that matches a partial word you're searching for. 或者,也许您的意思是想要找到与您正在搜索的部分单词匹配的任何单词。 regex can do that as well: 正则表达式也可以这样做:

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

This produces: 这会产生:

match 1: spoons
match 2: spo

There are a bunch of options here: 这里有很多选择:

a) Search for [space]WORD[space] instead of just WORD a)搜索[space]WORD[space]而不仅仅是WORD

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

Note that this wont work, if your words are separated by newline characters or other white spaces. 请注意,如果您的单词由换行符或其他空格分隔,则此操作无效。

b) Split the string into words, store them in a vector , and check if the desired word is somewhere in the vector, by using std::find . 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!";

If you're gonna search for words often, this maybe the best choice, since you can store the vector somewhere for future reference, so you don't have to split it. 如果你经常搜索单词,这可能是最好的选择,因为你可以将矢量存储在某个地方以供将来参考,所以你不必拆分它。 Also - if you want this to work, be sure to #include <algorithm> and #include <vector> . 另外 - 如果你想要这个,你一定要#include <algorithm>#include <vector>

c) Search for the word and check if isspace(string[position-1]) && isspace(string[position+wordLength]) 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!";
}

Something like this : 像这样的东西:

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;

See here 看到这里

I think the best way is to split your string using whitespace and punctuation characters as delimiters, then use std::find on the result. 我认为最好的方法是使用空格和标点符号作为分隔符来拆分字符串 ,然后在结果上使用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("\n\t .,!?\"()"));
    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;  
}  

This seems to have worked. 这似乎有效。

#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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM