dimanche 31 janvier 2016

Pass string as regex pattern input. (non-scalar error)

I am trying to perform a regex search, but instead of typing the pattern (e.g std::regex reg = "\.") I want to give regex a string input. Here is my code where I convert string to const char* and pass it to regex_search:

void trigger_regex(string name, string body)
    {

      map<string,string>::iterator it;
      it=test.mydata.find(name);
      if( it == test.mydata.end())
      {
        std::cout<<"REGEX not found"<<endl;
      }
      else
        cout << "Found REGEX with name: " << it->first << " and rule: " << it->second << endl ;

        string subject = body;
        string rule = it -> second;
        const char * pattern = rule.c_str();
        regex reg = (pattern);
        smatch match;    

        while (regex_search (subject,match,reg)) {
          for (auto x:match) std::cout << x << " ";
          std::cout << std::endl;
          body = match.suffix().str();
        }
     }

I get error:

conversion from 'const char*' to non-scalar type 'std::regex {aka std::basic_regex}' requested regex reg1 = (rule1);

any help pls?

Aucun commentaire:

Enregistrer un commentaire