如何在c++的字符串中找到一个完整的单词(不是它的一部分)
How do I find a complete word (not part of it) in a string in C++
在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;
}
相关文章:
- 选择基于另一个垫子的非零像素的cv::Mat的一部分?
- 复制矩阵的一部分并粘贴到另一个矩阵C++上
- 如何有效地将一个大std::字符串的一部分转换为float
- 从另一个常量标准::映射初始化一个常量标准::映射的一部分
- Makefile:编译一个llvm程序,作为g++编译的bnfc项目的一部分
- 将一个平台的一部分分享为另一个平台,同时将其涂成另一个平台
- 使用 C++98 将矢量的一部分移动(而不是复制)到另一个矢量
- 为什么将FMTFLAG指定两次 - 作为枚举的一部分,而另一个实例为静态const变量
- 如果将一个值复制到另一部分
- C++将 2D 阵列的一部分复制到另一个 2D 阵列,性能
- 如何制作一个C++映射容器,其中键是值的一部分
- C++将字符数组的一部分复制到另一个字符数组
- 如何在 c++ 中将一个向量的一部分复制到另一个向量
- 卡在测试'if':另一个代码的一部分
- 尝试在 Cocos2d-X C++中设置一个带有整数作为其字符串一部分的 CCLabelTTF
- 将内部类名声明为另一个声明的一部分
- Strchr不起作用,或者我需要一个替代方案来检查给定字符串中的任何字符是否是另一个字符串的一部分
- 把一个字符串的一部分分成更多的字符串
- 我怎样才能把这行的某一部分读成一个结构呢
- 将std::字符串的一部分复制到另一个未初始化的字符串