1

I need to implement a search mechanism for a list of words and give the result whether the word is found are not in a text provided.

I implemented this using boost library. And it is as follows,

int main(int argc, char**argv)
{
   int count = argc - 2;
   std::string text = argv[1];
   for(int i = 2; i < argc; i++)
   {
      boost::regex re(argv[i], boost::regex::icase);
      if(boost::regex_search(text, re))
      {
          std::cout<<re<<" Found in "<<text<<std::endl;
          count--;
      }
      else std::cout<<re<<" Not Found in " <<text<<std::endl;
   }
   if(count == 0)
   {
      std::cout <<"ALL WORDS ARE FOUND"<<std::endl;
   }
   else std::cout << "SOME WORDS ARE NOT FOUND"<<std::endl;
}

It is working fine for the command line arguements.

Now, what I need is I should provide an array of words directly to the boost search method (if any is available) as a parameter which should return whether all words are found are not.

I've Googled for this but of no use.

  • Why are you using regex? It seems to me like string::find() would work fine (http://en.cppreference.com/w/cpp/string/basic_string/find). – Nasser Al-Shawwa Jul 21 '14 at 11:14
  • 1
    ? @Nasser regular expressions have different semantics – sehe Jul 21 '14 at 11:14
  • @sehe I know. I was commenting on OP's implementation, but I missed the `boost::regex::icase` part. What I was suggesting is that `find()` would give the same result as the implementation. Anyway, my bad. – Nasser Al-Shawwa Jul 21 '14 at 11:17
  • @Nasser, from huge program which uses `boost` methods, I copied only a part which actually uses `regex_search`. – Shiva Kumar Ganthi Jul 21 '14 at 11:25
  • @Nasser you try that when arg[3] contains `"Ha.*!"`. And `text` contains `"Handsome Hallelujah Hazards!"`. `icase` is a mere detail here. Regex has vastly different semantics than `find` (which is probably why there are entire libraries implement regex search) – sehe Jul 21 '14 at 11:44

1 Answers1

1

Either just use an array:

 std::vector<std::string> arr { "many", "words", "in", "array" };
 int count = arr.size();

 std::string text = argv[1];
 for(std::string const& word : arr)
 {
    boost::regex re(text, boost::regex::icase);
    if(boost::regex_search(text, re))
    {
        std::cout<<re<<" Found in "<<text<<std::endl;
        count--;
    }
    else std::cout<<re<<" Not Found in " <<text<<std::endl;
 }

You could make a regular expression containing all the words you are looking for (this would (vastly) reduce runtime complexity, but makes it harder to count the presence of individual words).

sehe
  • 374,641
  • 47
  • 450
  • 633
  • Since you are using initializer lists, I presume that you are working with C++11, in which case I would suggest that you add some information about `std::regex` to OP – Nasser Al-Shawwa Jul 21 '14 at 11:21
  • @sehe, Thanks for your answer. But, I need a boost library function which directly takes `arr` as a parameter and returns me the output as `FOUND` or `Not Found`. – Shiva Kumar Ganthi Jul 21 '14 at 11:22
  • 3
    @Nasser std::regex is still widely unimplemented (at least GCC<4.9). I've no interest in suggesting less-supported, less featured (standard) libraries, when the OP is simply using Boost. – sehe Jul 21 '14 at 11:30
  • 2
    @ShivaKumarGanthi I need a function that I can call with arbitrary parameters and returns Unicorns. (Hint: _there is no silver bullet_; boost is a highly generic library and contains building blocks. You can be almost certain that Boost Regex functions do no take arrays as arguments anyways, so I think that's end of story then). – sehe Jul 21 '14 at 11:32
  • @sehe If there is no function which takes an array as an _arbitrary paramater_ then it is okay! – Shiva Kumar Ganthi Jul 21 '14 at 11:37