通过从文件读取来提升正则表达式匹配

时间:2014-11-21 05:36:06

标签: c++ regex boost

我在C ++中有以下代码,它应该逐行读取文件并匹配正则表达式以获取我正在寻找的信息。正则表达式由this link给出,但程序没有找到它。这是该计划:

#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;
            }
        }
    }

}

该文件应包含以下数据:

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

我的代码有什么问题吗?

1 个答案:

答案 0 :(得分:0)

根据Casimir et Hippolyte的建议,在我的正则表达式中添加两个反斜杠。即我的正则表达式是:

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