2012-04-22 67 views
0

我想在C++中取一個字符串,並找到其中包含的項目名稱和路徑。該字符串的格式爲:與VS2010的正則表達式C++/Boost

Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "OpenGL01", "OpenGL01\OpenGL01.vcxproj", "{65E58BFD-4339-44BA-BA9B-B2B8A5AC1FE1}" 

而正則表達式是:

boost::regex expression("(Project\(\"\{.*\}\"\)) ?= ?(\".*\"), ?(\".*\"), ?(\"\{.*\}\")"); 

,但此行產生異常錯誤:

Unhandled exception at 0x7512d36f in kmCompile2010.exe: Microsoft C++ exception: boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::regex_error> > at memory location 0x004ce13c..

這是完整的功能:

BOOL CSlnFile::ReadProjectsSolution() 
{ 
    CString sLine; 
    boost::regex expression("(Project\(\"\{.*\}\"\)) ?= ?(\".*\"), ?(\".*\"), ?(\"\{.*\}\")"); 

    BOOL bCheck = FALSE; 

    SeekToBegin(); 

    while(ReadString(sLine) && !bCheck) { 
     // CString to char* 
     CT2A temp(sLine);   // uses LPCTSTR conversion operator for CString and CT2A constructor 
     const char* pszA = temp; // uses LPSTR conversion operator for CT2A 
     std::string strA(pszA);  // uses std::string constructor 

     boost::smatch what; 

     if (regex_match(strA, what, expression, boost::match_extra)) { 
      // TODO 
     } 
    } 

    return bCheck; 
} 

我不是和我的錯誤。你可以幫我嗎?

+0

你需要雙精度除了引號之外的所有內容都可以轉義:'boost :: regex expression(「(Project \\(\」\\ {。* \\} \「\\))?=?(\」。* \「),? 「。* \」),?(\「\\ {。* \\}}」)「);' – ildjarn 2012-04-23 21:20:26

回答

0

我沒有嘗試下面的正則表達式中的boost ::正則表達式,它工作在Perl:

\{([^}]+)\}"[^"]*"([^"]+)", "([^"]+)", "\{([^}]+) 

我想它的命令行:

echo 'Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "OpenGL01", "OpenGL01\OpenGL01.vcxproj", "{65E58BFD-4339-44BA-BA9B-B2B8A5AC1FE1}"' | perl -ne 'my ($a, $b, $c, $d) = /\{([^}]+)\}"[^"]*"([^"]+)", "([^"]+)", "\{([^}]+)/; print "$a, $b, $c, $d"' 
+0

嗨。隨着升壓不起作用。我有同樣的例外。 – 2012-04-22 11:46:29