请问我该如何优化下面这段c++的正则


   
  if(itor2->second == string("sb3"))
  
{
regex_ = new regex("^\.xml$");
if(regex_match(char_1,*regex_))
{
(*map_url)[string((*i).str())] = string("xml");
delete regex_;
continue;
}
else
{
delete regex_;
}

///* regex_ = new regex("html");
// if(regex_match(wei.c_str(),*regex_))
// {
// (*map_url)[string((*i).str())] = "html";
// delete regex_;
// continue;
// }
// */

regex_ = new regex("^\.jpg$");
if(regex_match(char_1,*regex_))
{
(*map_url)[string((*i).str())] = string("jpg");
delete regex_;
continue;
}
else
{
delete regex_;
}

regex_ = new regex("^\.png$");
if(regex_match(char_1,*regex_))
{
(*map_url)[string((*i).str())] = string("png");
delete regex_;
}
else
{
delete regex_;
}

regex_ = new regex("^\.css$");
if(regex_match(char_1,*regex_))
{
(*map_url)[string((*i).str())] = string("css");
delete regex_;
continue;
}
else
{
delete regex_;
}

regex_ = new regex("^\.\.js$");
if(regex_match(char_1,*regex_))
{
(*map_url)[string((*i).str())] = string("js");
delete regex_;
continue;
}
else
{
delete regex_;
}

这段是for循环中 主要是有个char数组 长度是 4 然后匹配 文件类型
如果用一句正则如何实现
不想用捕获
例如
regex_ = new regex("(^..js$)?(^.css$)?");
请问还有更好的方法 么 ?

正则表达式 C++

Ayaくん 12 years, 7 months ago

Your Answer