将多个组与CAtlRegExp匹配

Match more than one group with CAtlRegExp

本文关键字:CAtlRegExp 匹配      更新时间:2023-10-16

我正在尝试使用youtube数据api获取youtube播放列表的总持续时间。例如,我从,http://gdata.youtube.com/feeds/api/playlists/63F0C78739B09958我的想法是迭代每个<yt:duration='xxx'/>事件,其中xxx是以秒为单位的每个视频持续时间,并将它们相加以获得总播放列表运行时间。

为了获得每个,我使用CAtlRegExp和以下字符串:

<yt:duration seconds='{[0-9]+}'/>

然而,它只匹配第一次出现的内容,而不匹配其他内容(参考下面粘贴的源代码,循环只迭代一次)。

我尝试了一些其他regex字符串,如

  • (<yt:duration seconds='{[0-9]+}'/>)

  • (<yt:duration seconds='{[0-9]+}'/>)*

然而,它们也不起作用(同样的原因)。

以下是源代码的摘录,其中for循环只迭代一次,因为mcDuration.m_uNumGroups等于1:

    //get video duration
    CAtlRegExp<> reDurationFinder;
    CAtlREMatchContext<> mcDuration; 
    REParseError status = reDurationFinder.Parse(_T("<yt:duration seconds='{[0-9]+}'/>"));
    if ( status != REPARSE_ERROR_OK )
    {
        // Unexpected error.
        return false;
    }
    if ( !reDurationFinder.Match(sFeed, &mcDuration) ) //i checked it with debug, sFeed contains full response from youtube data api
    {
        //cannot find video url
        return false;
    }
    m_nLengthInSeconds = 0;
    for ( UINT nGroupIndex = 0; nGroupIndex < mcDuration.m_uNumGroups; ++nGroupIndex )
    {
        const CAtlREMatchContext<>::RECHAR* szStart = 0;
        const CAtlREMatchContext<>::RECHAR* szEnd = 0;
        mcDuration.GetMatch(nGroupIndex, &szStart, &szEnd);
        ptrdiff_t nLength = szEnd - szStart;
        m_nLengthInSeconds += _ttoi(CString(szStart, nLength));
    }

如何使CAtlRegExp<yt:duration ...的所有出现相匹配?

您总是只出现第一次(下一次)。要查找其他事件,您需要保持Match'ing循环,直到找不到其他事件为止。

    for(; ; )
    {
        CAtlREMatchContext<> MatchContext;
        pszNextText = NULL;
        if(!Expression.Match(pszText, &MatchContext, &pszNextText))
            break;
        // Here you process the found occurrence
        pszText = pszNextText;
    }