2016-09-20 15 views
-2

私は.slnファイルを持っており、データを取得する必要があります。 .slnファイルはthatのように見えます。このファイルからプロジェクトの名前が必要です。また、正しく見える場合、私は撮影文字列で動作するコードを持っています。だから私は正規表現を作ろうとしましたC++の正規表現が正しくありません

"プロジェクト\ + \ "([\ 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; 
     } 
    } 
} 

私はこのコードをしようとしますそれは間違いがあると言います。私はそれをどのように修正するか分かりません。

エラーがあります。Microsoft C++の例外:メモリ位置0x0031E890でのstd :: regex_error ConsoleApplication1.exeで0x775EC42Dで

未処理の例外。

+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 - 私はしばらくの間ランチに行くつもりです、私は知っている。 –