Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将字符串作为正则表达式模式输入传递。(非标量错误)_C++_Regex_String_C++11 - Fatal编程技术网

C++ 将字符串作为正则表达式模式输入传递。(非标量错误)

C++ 将字符串作为正则表达式模式输入传递。(非标量错误),c++,regex,string,c++11,C++,Regex,String,C++11,我试图执行正则表达式搜索,但不是键入模式(例如std::regex reg=“\”),而是为正则表达式提供字符串输入。下面是我的代码,我将字符串转换为const char*并将其传递给regex_search: void trigger_regex(string name, string body) { map<string,string>::iterator it; it=test.mydata.find(name); if( it =

我试图执行正则表达式搜索,但不是键入模式(例如std::regex reg=“\”),而是为正则表达式提供字符串输入。下面是我的代码,我将字符串转换为const char*并将其传递给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();
        }
     }
void触发器\u正则表达式(字符串名称、字符串正文)
{
对它进行迭代器;
it=test.mydata.find(名称);
if(it==test.mydata.end())
{
std::cout如果您查看,例如,您将看到仅使用字符串的构造函数(列表中的第二个构造函数)标记为
explicit
。这意味着您必须显式使用该构造函数,您不能只希望将字符串转换为
std::regex
对象

换句话说,解决方案是像这样定义和初始化对象

std::regex reg1{pattern};