IT虾米网

c++之ATL正则表达式解析csv文件

davidwang456 2025年05月04日 程序员 20 0

有人可以告诉我下面的代码有什么问题吗?我正在尝试使用下面的程序解析 CSV 文件,但它在 m_uNumGroups 字段中返回零。

int _tmain(int argc, _TCHAR* argv[]) 
{ 
     CAtlRegExp<> reUrl; 
    // Five match groups: scheme, authority, path, query, fragment 
    REParseError status = reUrl.Parse(**L"[^\",]+|(?:[ˆ\"])|\"\")"**); 
 
    if (REPARSE_ERROR_OK != status) 
    { 
        // Unexpected error. 
        return 0; 
    } 
 
    TCHAR testing[ ] = L"It’ s \" 10 Grand\" , baby"; 
 
    CAtlREMatchContext<> mcUrl; 
    if (!reUrl.Match(testing,&mcUrl)) 
    { 
        // Unexpected error. 
        return 0; 
    } 
 
    for (UINT nGroupIndex = 0; nGroupIndex < mcUrl.m_uNumGroups;nGroupIndex) 
    { 
        const CAtlREMatchContext<>::RECHAR* szStart = 0; 
        const CAtlREMatchContext<>::RECHAR* szEnd = 0; 
        mcUrl.GetMatch(nGroupIndex, &szStart, &szEnd); 
 
        ptrdiff_t nLength = szEnd - szStart; 
        printf_s("%d: \"%.*s\"\n", nGroupIndex, nLength, szStart); 
    } 
 
    return 0;; 
} 

请您参考如下方法:

使用 ATL 正则表达式语法,您需要在要捕获的表达式周围使用大括号。您的表达式没有任何表达式,因此您只需进行没有 sbu 表达式的匹配。

看看这个:http://msdn.microsoft.com/en-us/library/k3zs4axe%28v=vs.80%29.aspx

{ } Indicates a match group. The actual text in the input that matches the expression inside the braces can be retrieved through the CAtlREMatchContext object.


评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!