要分析一些程序输出的LOG文件,里面的错误代码是一些整数值,这些值在头文件中以这两种形式定义:
//#define ARN_SUCCESS 0l  //ARRON!'HAHA
//#define Arn_error1  ARN_SUCCESS + 0X0F  //AARON ERROR1现在想做一个小程序来做查询,当输入15时,可以给出Arn_error1这个错误标题和Aaron Error1这个详细说明,使用正则表达式做。
我使用static std::string sMacroPtn = "#define\\s+([^\\s]+)\\s+([^\\n/]+)(?://)*(.*)";来匹配上面两种串,这样得到的子匹配串中,\1要么为宏的值,要么为宏求值的表达式。所以要进一步地匹配:
boost::regex reExpression("(\\w+)\\s+\\+\\s+(\\w+)");
boost::regex reDirectNumber("(?:0x|0X)*([a-f0-9A-F]+)(?:l|L)*");int ResolveMsgValue(std::string& strTitle, std::string& strExpression)
{
static CMap<CString, const char*, int, int> mapMsg;

boost::match_results<std::string::const_iterator> what;
if (boost::regex_match(strExpression, what, reDirectNumber))
{
int i = strtoul(strExpression.c_str(), NULL, 0);
mapMsg.SetAt(strTitle.c_str(), i);
return i;
}

if(boost::regex_match(strExpression, what, reExpression))
{
int iBase = 0;
if (!mapMsg.Lookup(what[1].str().c_str(), iBase))
return 0; //unrecognizable expression

int iResult = iBase + strtoul(what[2].str().c_str(), NULL, 0);
mapMsg.SetAt(strTitle.c_str(), iResult);
return iResult;
}
else
{
int i = strtoul(strExpression.c_str(), NULL, 0);
mapMsg.SetAt(strTitle.c_str(), i);
return i;
}

return 0;
}在调用中发现,//#define ARN_SUCCESS 0l  //ARRON!'HAHA
这一句被匹配到,且子匹配\1的值为0l,但无法通过boost::regex_match(strExpression, what, reDirectNumber)这一匹配,为什么?
为什么第二句不能被匹配到?