2016-09-20 86 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 - 如果你需要更多的幫助,我會去吃午飯,我知道。 –