使用 VS2010 C++/增强的正则表达式

Regular Expression with VS2010 C++ / Boost

本文关键字:增强的 正则表达式 C++ VS2010 使用      更新时间:2023-10-16

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

Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "OpenGL01", "OpenGL01OpenGL01.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;
}

我没有发现我的错误。你可以帮我吗?

我没有在 boost::regex 中尝试以下正则表达式,它在 perl 中工作:

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

我在命令行上尝试过:

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