2008年2月26日星期二

Boost regex backslash 问题

regex eps_change_blash("\"); //throw exception causes of a "bad_pattern".The problem is that
//have to escape the backslash once because of how C literals are interpreted
//and again because of how characters in regular expressions are interpreted.

Lösung:
regex eps_change_blash("\\");
or:
boost::regex expression("\", boost::regbase::literal);






Beispiel: use sourceDB(c:\backup\dbinfo.mdb) to repalce c:/apo_work/dbinfo.mdb
comes the problem "c:backupdbinfo.mdb"
Lösung: \\\\ for just once \

CString CDBinfoListCt::replaceSourceDB(CString s)
{
regex eps_sql_sourceDB("^.*\'c:\/apo_work\/dbinfo\.mdb\'$");
regex eps_sql_change_sourceDB("(c:/apo_work/dbinfo\.mdb)");
regex eps_change_blash("\\\\"); // \\\\ backslash for c, for boost... god!!!

cmatch what; // a array to store the matching string

std::string stdReplace;
CString sTmp;
CString sTmpDB;


if(regex_match(s, what, eps_sql_sourceDB))
{


stdReplace = (LPCTSTR)sourceDB;
stdReplace = boost::regex_replace(stdReplace,eps_change_blash,"\\\\\\\\");
sTmpDB.Format("%s",stdReplace.c_str());

sTmp.Format("%s",what[0]);
stdReplace = (LPCTSTR)sTmp;
stdReplace = boost::regex_replace(stdReplace,eps_sql_change_sourceDB,(LPCTSTR)sTmpDB);

sTmp.Format("%s",stdReplace.c_str());
return sTmp;

}
else
{
return s;

}


}

没有评论: