1

I have a problem using the regular expression in cpp. I want to find the some keyword in the string. For ex, if the keyword was 'blind' it doesn't match 'ablind' or 'blindblind' also ignore whether its upper or lower.

So i used the regular expression like (!:\b|[^a-zA-Z])keyword is written here(!=\b|[^a-zA-Z]). My idea is correct? and I wonder where are the websites having lots of info about using regex in c++

#include <string>
#include <regex>

int solution(string word, string str);

int main(){

  string word = "muzi";
  string str = "Muzidajkld mUzi muzimuzim bdjkleamcdj muzI";
  int n = solution(word, str);
  //Value of n must be 2(only mUzi and muzI are matched)
  return 0;
 }

int solution(string word, string str){
  //returns how many times 'word' appear in str.
   string reg_str = "(!:\\b|[^a-zA-Z])"+word+"(!=\\b|[^a-zA-Z])";
   regex re(reg_str, regex::icase | regex::ECMAScript);

   auto start = regex_iterator(str.begin(), str.end(), re);
   auto end = regex_iterator();

   int answer = distance(start, end);
   return answer;
  }
papayetoo
  • 25
  • 7

0 Answers0