确定给定句子是否包含给定单词c++; 我需要确定在C++中是否在另一个字符串中找到给定的单词。< /P>
我的函数原型将像确定给定句子是否包含给定单词c++; 我需要确定在C++中是否在另一个字符串中找到给定的单词。< /P>,c++,regex,string,substring,C++,Regex,String,Substring,我的函数原型将像bool-check(字符串句子,字符串单词) “句子”可以是这样的:word.someWord和/或/XOR-word2.someWord*和/或/XOR-word3.someWord就是这样的 举一个真实的例子:打开车门。值=1,然后转动汽车。校验和=1 现在,在这个例子中,若我将单词分配给lock,我的check函数应该返回false,但若我将其设置为“unlock”,它应该返回true 为此,我使用了.find()函数,但大家都知道它是错误的,因为它将我返回为true,即
bool-check(字符串句子,字符串单词)代码>
“句子”可以是这样的:word.someWord和/或/XOR-word2.someWord*和/或/XOR-word3.someWord
就是这样的
举一个真实的例子:打开车门。值=1,然后转动汽车。校验和=1
现在,在这个例子中,若我将单词分配给lock,我的check函数应该返回false,但若我将其设置为“unlock”,它应该返回true
为此,我使用了.find()函数,但大家都知道它是错误的,因为它将我返回为true,即使我将lock作为word参数,因为“unlock”包含“lock”
我想我需要使用正则表达式,但我不知道怎么做。有人帮我吗?提前谢谢 首先将你的句子分成几个单词,然后检查列表,看看是否包含你要查找的确切单词
你也可以考虑使用正则表达式,并在每一行之前查找你的单词,后面跟着任何空格、标点或结尾。
< P>我的意思是下划线是空白。< /P>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <iterator>
#include <algorithm>
bool check( const std::string &sentence, const std::string &word )
{
std::istringstream is( sentence );
return std::find( std::istream_iterator<std::string>( is ),
std::istream_iterator<std::string>(),
word ) != std::istream_iterator<std::string>();
}
int main()
{
std::cout << std::boolalpha
<< check( "unlock the door.value=1 AND turn of car.checkSum=1", "lock" )
<< std::endl;
std::cout << std::boolalpha
<< check( "unlock the door.value=1 AND turn of car.checkSum=1", "unlock" )
<< std::endl;
return 0;
}
如果下划线不是空白,则可以使用相同的std::istringstream
和标准函数std::getline
,并检查每个读取字符串是否等于给定字符串
或者您确实可以使用成员函数find\u first\u of
比如说
#include <iostream>
#include <iomanip>
#include <string>
bool check( const std::string &sentence, const std::string &word )
{
const char *delimiters = " _=.";
std::string::size_type first, last;
bool found = false;
first = 0;
while ( !found && first != std::string::npos )
{
first = sentence.find_first_not_of( delimiters, first );
if ( first != std::string::npos )
{
last = sentence.find_first_of( delimiters, first );
found = sentence.substr( first, last == std::string::npos ? last : last - first ) == word;
first = last;
}
}
return found;
}
int main()
{
std::string s = "unlock_the_door.value=1 AND turn_of_car.checkSum=1";
std::cout << std::boolalpha
<< check( s, "lock" )
<< std::endl;
std::cout << std::boolalpha
<< check( s, "unlock" )
<< std::endl;
return 0;
}
用下划线分隔句子作为定界符。
并使用stricmp函数。
也许这就解决了。如果我没弄错的话,你想看看这个词是否不仅包含在这个句子中,而且也包含在这个句子中。
您可以这样做:
bool check (std::string sentence, std::string word)
{
std::string part;
for(unsigned int ii=0; ii<sentence.size()-word.size(); ii++)
{
part = sentence.substr(ii,word.size());
if(!part.compare(word)) {
return true;
}
}
return false;
}
仅当您检查“解锁”而不是“锁定”时返回true。但是,通过指定分隔符,您可以指定希望作为单词处理的内容和不希望作为单词处理的内容。您可以使用C++11regex\u search
作为搜索模式,使用由单词边界(\b
)或下划线包围的单词
#include <iostream>
#include <string>
using namespace std;
bool check(const string &sentence, const string &word) {
string boundary = "(_|\\b)";
return regex_search(sentence, regex(boundary + word + boundary));
}
int main () {
string sentence = "unlock_the_door.value=1 AND turn_of_car.checkSum=1";
cout << check(sentence, "lock") << endl;
cout << check(sentence, "unlock") << endl;
}
#包括
#包括
使用名称空间std;
布尔检查(常量字符串和句子、常量字符串和单词){
字符串边界=“(124; \\ b)”;
返回正则表达式搜索(句子,正则表达式(边界+单词+边界));
}
int main(){
字符串句子=“解锁车门。值=1,转动车门。校验和=1”;
我能问一下-1的原因是什么吗?你不是在提出一个尝试性的解决方案并就此提出一个问题;你是在提出一个解决方案并要求它为你的客户实施(至少,在我看来是这样的)。在使用基于正则表达式的实现之前,您应该至少了解一些正则表达式语法;否则,您得到的任何解决方案都可能无法使用(或可用但非常脆弱)对你来说,无论是哪种方式,你都在问C++的正则表达式API,使用的实际正则表达式语法,还是别的什么?实际上,我想知道有没有一种方法可以在不使用正则表达式的情况下完成这一操作;你可以标记字符串并处理每个令牌(见@史蒂文巴恩斯的答案)。,您可以为您的缓冲区编写一个全状态前向解析器(作为FSM实现-更高效但更复杂/更容易出错),您可以使用regex(高抽象级别)或上述组合。我将使用一种组合:首先将表达式标记为术语(“解锁车门。值=1”,“和”,“打开车门。校验和=1”)在每个术语的标识符上加上一个正则表达式。它不是最有效的,但是它是一个很好的分离关注点,并且很容易在以后修改/扩展。(如果你必须在一个非常苛刻的环境中这样做,考虑写一个字符单程分析器(作为一个FSM))。@Steve_Barnes感谢您的回复,如果我将我的句子拆分为单词,例如上面的示例,它将类似于word1=unlock_tw_door.value=1 word2=和word3=turn_of_car.checkSum=1,再次查找函数将为锁字返回true,因为word1包含它。A希望您匹配列表中的单词,而不是使用列表中的“unlock”字!=“lock”@Vlad_from_Moscow下划线不是空格,它们是单词的一部分。但是我不能给出你的建议。你能根据它编辑它吗?@eday查看我的更新帖子。我想你不明白我的问题,所以请不要推它。你的第二个函数每次调用都会泄漏内存。它使用new[]分配内存
并且永远不要使用delete[]
释放它。
bool check (std::string sentence, std::string word)
{
std::string part;
for(unsigned int ii=0; ii<sentence.size()-word.size(); ii++)
{
part = sentence.substr(ii,word.size());
if(!part.compare(word)) {
return true;
}
}
return false;
}
bool check (std::string sentence, std::string word)
{
char *c_str_sentence = new char[sentence.size()+1]; //you need this cause string.c_str() will return const char* but strtok needs char*;
char *c_str_word = new char[word.size()+1];
strcpy(c_str_sentence,sentence.c_str());
strcpy(c_str_word,word.c_str());
bool is_contained = false;
const char *delimiters = " _=."; //any delimiter you wish;
char *part = strtok(c_str_sentence,delimiters);
while (part != NULL)
{
if(!strcmp(part,c_str_word)) {
is_contained = true;
break;
}
part = strtok(NULL,delimiters);
}
delete[] c_str_sentence;
delete[] c_str_word;
return is_contained;
}
#include <iostream>
#include <string>
using namespace std;
bool check(const string &sentence, const string &word) {
string boundary = "(_|\\b)";
return regex_search(sentence, regex(boundary + word + boundary));
}
int main () {
string sentence = "unlock_the_door.value=1 AND turn_of_car.checkSum=1";
cout << check(sentence, "lock") << endl;
cout << check(sentence, "unlock") << endl;
}
#include <boost/regex.hpp>
using namespace boost;