简体   繁体   English

如何使String :: Find(is)省略

[英]How to make String::Find(is) omit this

If I have a list, which contains the 4 nodes ("this"; "test example"; "is something of"; "a small") and I want to find every string that has "is" (only 1 positive with this list). 如果我有一个包含4个节点的列表(“ this”;“ test example”;“ is something of”;“ small”),并且我想查找每个具有“ is”的字符串(此字符串中只有1个正数)列表)。 This topic has been posted a large number of times, which I have used to help get me this far. 这个话题已经发布了很多次,我一直以来都在帮助我。 However, I can't see anywhere how I omit "this" from a positive result. 但是,我在任何地方都看不到如何从积极结果中忽略“这个”。 I could probably use string::c_str, then find it myself, after I've reduced my much larger list. 我可以使用string :: c_str,然后在减少大得多的列表之后自己找到它。 Or is there a way I could use string::find_first_of? 还是有一种方法可以使用string :: find_first_of? It would seem there's a better way. 似乎有更好的方法。 Thanks. 谢谢。
EDIT: I know that I can omit a particular string, but I'm looking for bigger picture b/c my list is quite large (ex: poem). 编辑:我知道我可以省略特定的字符串,但是我正在寻找更大的图片b / c,我的列表很大(例如:poem)。

for(it = phrases.begin(); it != phrases.end(); ++it)
{
    found = it->find(look);
    if(found != string::npos)
        cout << i++ << ". " << *it << endl;
    else
    {
        i++;
        insert++;
    }
}

Just to clarify: what are you struggling with? 只是要澄清一下:您在努力吗?

What you want to do is check if what you have found is the start of a word (or the phrase) and is also the end of a word (or the phrase) 您要做的就是检查您找到的内容是否是单词(或短语)的开头,还是单词(或短语)的结尾

ie. 即。 check if: 检查:

  • found is equal to phrases.begin OR the element preceding found is a space found内容等于phrases.beginfound的前面的元素是一个空格
  • AND two elements after found is a space OR phrases.end 并且在found两个元素之后是一个空格或phrases.end

EDIT: You can access the character that was found by using found (replace X with the length of the string you're finding (look.length) 编辑:您可以使用found的字符来访问找到的字符(将X替换为要查找的字符串的长度(look.length)

found = it->find(look);
if(found!=string::npos)
{
    if((found==0 || it->at(found-1)==' ')
        && (found==it->length-X || it->at(found+X)==' '))
    {
         // Actually found it
    }
} else {
    // Do whatever
}

I didn't realize you only wanted to match "is". 我没有意识到您只想匹配“是”。 You can do this by using an std::istringstream to tokenize it for you: 您可以通过使用std :: istringstream为其代币化来做到这一点:

std::string term("is");

for(std::list<std::string>::const_iterator it = phrases.begin();
    it != phrases.end(); ++it)
{
    std::istringstream ss(*it);
    std::string token;
    while(ss >> token)
    {
        if(token == term)
            std::cout << "Found " << token << "\n";
    }
}

We can use boost regex for searching regular expressions. 我们可以使用boost regex搜索正则表达式。 Below is an example code. 以下是示例代码。 Using regular expression complex seacrh patterns can be created. 使用正则表达式可以创建复杂的seacrh模式。

#include <boost/regex.hpp> 
#include <string> 
#include <iostream> 
#include  <boost/tokenizer.hpp>

using namespace boost;
using namespace std;

int main()
{
  std::string list[4] = {"this","hi how r u ","is this fun is","no"};

  regex ex("^is"); 

  for(int x =0;x<4;++x)
  {
    string::const_iterator start, end;
    boost::char_separator<char> sep(" ");
    boost::tokenizer<boost::char_separator<char> > token(list[x],sep);

    cout << "Search string:  " << list[x] <<"\n"<< endl;
    int x = 0;
    for(boost::tokenizer<boost::char_separator<char> >::iterator itr = token.begin();
        itr!=token.end();++itr)
    {
      start = (*itr).begin();
      end = (*itr).end();

      boost::match_results<std::string::const_iterator> what;
      boost::match_flag_type flags = boost::match_default;

      if(boost::regex_search(start, end, what, ex, flags))
      {
        ++x;
        cout << "Found--> " << what.str() << endl;
      }
    }

    cout<<"found pattern "<<x <<" times."<<endl<<endl;
  }
  return 0;
}

Output: 输出:

Search string: this 搜索字符串:此

found pattern 0 times. 找到模式0次。

Search string: hi how ru 搜索字符串:嗨如何ru

found pattern 0 times. 找到模式0次。

Search string: is this fun is 搜索字符串:这很有趣吗

Found--> is Found--> is found pattern 2 times. 找到->被发现->被发现模式2次。

Search string: no 搜索字符串:否

found pattern 0 times. 找到模式0次。

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

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