1

I need to parse log and I`ve good working regex, but now I need to set regex from config file and here is problem.

int logParser()
{
  std::string bd_regex; // this reads from config in other part of program
  boost::regex parsReg;
  //("(C:.tmp.bd.*?)+(([a-zA-Z0-9_]+\\.)+[a-zA-Z]{2,4})+(.+[a-zA-Z0-9_])");
  try
  {
    parsReg.assign(bd_regex, boost::regex_constants::icase);  
  }
  catch (boost::regex_error& e)
  {
    cout << bd_regex << " is not a valid regular expression: \""
         << e.what() << "\"" << endl;
  }

  cout << parsReg << endl;
  // here it looks exactly like:
  // "("(C:.tmp.bd.*?)+(([a-zA-Z0-9_]+\\.)+[a-zA-Z]{2,4})+(.+[a-zA-Z0-9_])");"

  int count=0;
  ifstream in;

  in.open(bd_log_path.c_str());

  while (!in.eof()) 
  {
    in.getline(buf, BUFSIZE-1);
    std::string s = buf;
    boost::smatch m;

    if (boost::regex_search(s, m, parsReg)) // it doesn't obey this "if"
    {
      std::string name, diagnosis;
      name.assign(m[2]);
      diagnosis.assign(m[4]);

      strcpy(bd_scan_results[count].file_name, name.c_str());
      strcpy(bd_scan_results[count].out,  diagnosis.c_str());
      strcat(bd_scan_results[count].out,  " ");

      count++;
      } 
    }
  return count;
}

and I really dont know why the same regex dont work when I tryed to set it from config variable.

Any help will be appreciated (:

4

2 回答 2

0

关于您的直接问题:尝试在配置文件中存储不带转义的正则表达式

(C:.tmp.bd.*?)+(([a-zA-Z0-9_]+\.)+[a-zA-Z]{2,4})+(.+[a-zA-Z0-9_])

此外,我必须说,您似乎想在此处匹配反斜杠:

C:.tmp.bd.

在配置中,写:

C:\\tmp\\bd\\

在 C++ 字符串文字中

"C:\\\\tmp\\\\bd\\\\"

于 2011-11-17T23:28:21.383 回答
0

@sehe 给出了正确的答案。

如果这行代码被c++解析器解析,
str = "(C:.tmp.bd.*?)+(([a-zA-Z0-9_]+\\.)+[a-zA-Z]{2,4})+(.+[a-zA-Z0-9_])";

它将转义字符转义为转义符\\\,然后
将其分配给变量“str”。在变量“str”内部,它现在看起来像这样:
(C:.tmp.bd.*?)+(([a-zA-Z0-9_]+\.)+[a-zA-Z]{2,4})+(.+[a-zA-Z0-9_])

但是,您正在从文件中读取此文本,在语言意义上没有解析。
您正在分配给'str',一个原始文本行。c++ 解析器未预处理的行。

于 2011-11-18T00:56:47.303 回答