2016-09-20 51 views
-2

我有一个.sln文件,我需要从中获取一些数据。 .sln文件看起来像that。我需要这个文件的项目名称。此外,我有一个代码,将采取采取字符串,如果它看起来是正确的。所以我试着做一个正则表达式c + +中的正确​​表达式不正确

“Project(\”{[\ w-] +} \“)\ s * = \ s * \”(\ w +)\“[,\ s] + \ “([\ W \] * vcxproj。)\”[,\ S] + \ “({[\ W - ] +})\””

#include <iostream> 
#include <string> 
#include <vector> 
#include <regex> 
#include <fstream> 

using namespace std; 

int main() 
{ 
    ifstream ifstr("C:\\Users\\Andrew\\Documents\\Visual Studio 2015\\Projects\\ConsoleApplication1\\ConsoleApplication1.sln"); 
    string all; 
    //Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "doctor", "dreryk\src\doctor\doctor.vcproj", "{5D031DBA-1903-4067-A2CE-01B104A08D48}" 
    regex projParse("Project\(\"\{[\w-]+\}\"\)\s*=\s*\"(\w+)\"[,\s]+\"([\w\\]*\.vcxproj)\"[,\s]+\"(\{[\w-]+\})\""); 
    ifstr.seekg(0); 
    string pth, sol; 
    match_results<string::const_iterator> what; 
    while (getline(ifstr, sol)) 
    { 
     string::const_iterator start = sol.begin(); 
     string::const_iterator end = sol.end(); 
     if (regex_search(start, end, what, projParse)) 
     { 
      cout << what[0] << endl; 
     } 
    } 
} 

当我尝试这个代码它说有一个错误。我不知道如何解决它。

错误是:微软C++异常:

在0x775EC42D在ConsoleApplication1.exe中未处理的异常的std :: regex_error内存位置0x0031E890。

+0

什么是实际你得到的错误? – NathanOliver

+5

转义正则表达式字符串文字中的斜杠。 –

+0

我怎么能逃脱他们? – koshachok

回答

1

所以,我给了这个去,我相信我简化了你的正则表达式。下面是一些代码:

#include <iostream> 
#include <string> 
#include <regex> 
#include <fstream> 

using namespace std; 


void show_matches(const std::string& in, const std::string& re) 
{ 
    smatch m; 
    regex_search(in, m, std::regex(re)); 
    if(m.empty()) { 
     cout << "input=[" << in << "], regex=[" << re << "]: NO MATCH\n"; 
    } else { 
     cout << "input=[" << in << "], regex=[" << re << "]: "; 
     cout << "prefix=[" << m.prefix() << "] "; 
     for(size_t n = 0; n < m.size(); ++n) 
      cout << " m[" << n << "]=[" << m[n] << "] "; 
     cout << "suffix=[" << m.suffix() << "]\n"; 
    } 
} 

int main() 
{ 

    show_matches("Project(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\") = \"doctor\", \"dreryk\\src\\doctor\\doctor.vcproj\\\", \"{5D031DBA-1903-4067-A2CE-01B104A08D48}\"", 
    "Project\\(\"\\{([^\\}]*)\\W+(\\w+)\\W+(.*).vcproj\\W+([^\\}]*)\\W+"); 
} 

而正则表达式:

Project\(\"\{([^\}]*)\W+(\w+)\W+(.*).vcproj\W+([^\}]*)\W+ 

拆毁了

Project\(\"\{ -- Literally match Project("{ 
([^\}]*) -- Capture group 1: Capture all characters until } 
\W+ -- Eat all non-letter characters until the next capture group 
\w+ -- Capture group 2 -- Eat everything until non character (in this case ") 
\W+ -- Eat all non-letter characters until we get to our next capture group 
(.*).vcproj -- Capture group 3 eat everything until .vcproj 
\W -- Eat everything until our last capture group 
([^\}]*) -- Capture group 4 - eat everything until } 
\W+ Eat until the end of the string. 

输入:

Project(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\") = \"doctor\", \"dreryk\\src\\doctor\\doctor.vcproj\\\", \"{5D031DBA-1903-4067-A2CE-01B104A08D48}\"" 

输出:

prefix=[] m[0]=[Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "doctor", "dreryk\src\doctor\doctor.vcproj\", "{5D031DBA-1903-4067-A2CE-01B104A08D48}"] m[1]=[8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942] m[2]=[doctor] m[3]=[dreryk\src\doctor\doctor] m[4]=[5D031DBA-1903-4067-A2CE-01B104A08D48] suffix=[] 

上面的代码是伟大的,在C++中,如果你需要它的测试正则表达式 - 它是从代码衍生的cppreference.com(信贷,这是由于)

好运

+0

您是否在电脑上试过这段代码?因为我有一些问题) – koshachok

+0

我给你做了一个快速测试仪,它可以帮助你:现在把它添加到我的答案中 –

+0

@koshachok - 如果你需要更多的帮助,我会去吃午饭,我知道。 –