使用VS2010 C ++ / Boost进行正则表达式

时间:2012-04-22 10:17:24

标签: c++ visual-studio-2010 visual-c++ boost boost-regex

我正在尝试在C ++中获取一个字符串,并找到其中包含的项目名称和路径。该字符串的格式为:

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

正则表达式为:

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

但是这一行会产生异常错误:

  

kmCompile2010.exe中0x7512d36f处的未处理异常:Microsoft C ++异常:内存位置为0x004ce13c的boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::regex_error> > ..

这是完整的功能:

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

我没有发现我的错误。你能救我吗?

1 个答案:

答案 0 :(得分:0)

我没有在boost :: regex中尝试以下正则表达式,它在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"'