2014-11-21 74 views
1

我在C++中有以下代碼,它應該逐行讀取文件並匹配正則表達式以獲取我正在尋找的信息。該正則表達式的有效性由this link給出,儘管程序沒有找到它。這裏的程序:Boost正則表達式與文件讀取匹配

#include <boost/regex.hpp> 
#include <iostream> 
#include <fstream> 
int main() 
{ 
    boost::regex expr1("Property\(C\):\sIP\s\=(.*)\n"); 
    boost::smatch what1; 
    std::string line; 
    std::ifstream myfile("document.txt"); 
    if(myfile.is_open()){ 
     std::cout <<"FILE OPEN " <<std::endl; 
     while(getline(myfile, line)) 
     { 
      std::cout <<" LINE : " << line <<std::endl; 
      if (boost::regex_search(line, what1, expr1)) 
      { 
       std::cout<<"MATCH FOUND " <<std::endl; 
      for(int i(0); i<what1.size(); i++) 
       std::cout << "WHAT " <<i<<" "<<what1[i] <<std::endl; 
      } 
     } 
    } 

} 

該文件應該包含以下DATAS:

Property(C): IP = 127.0.0.1 
Property(C): PORT = 9999 
Property(C): CURRENTDIRECTORY = C:\Users\logpoint\Downloads\Compressed\command_lines_and_setups_source\Setup\Debug 
Property(C): CLIENTUILEVEL = 0 
Property(C): CLIENTPROCESSID = 28184 
Property(C): VersionDatabase = 200 

這有什麼錯在我的代碼?

+2

無需轉義'=',它不是特殊字符。您需要使用雙反斜槓而不是簡單的反斜槓。 – 2014-11-21 05:50:23

+0

我是否應該爲空格字符添加雙反斜槓 – Pant 2014-11-21 05:51:57

+0

是的,是否添加兩個反斜槓表示謝謝。 – Pant 2014-11-21 05:53:09

回答

0

正如Casimir和Hippolyte所建議的那樣,在我的正則表達式中添加兩個反斜槓。即我的正則表達式是:

"Property\\(C\\):\\sIP\\s=(.*)"