C++ 将字符串作为正则表达式模式输入传递。(非标量错误)
我试图执行正则表达式搜索,但不是键入模式(例如std::regex reg=“\”),而是为正则表达式提供字符串输入。下面是我的代码,我将字符串转换为const char*并将其传递给regex_search: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 =
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};