【问题标题】:Match more than one group with CAtlRegExp使用 CAtlRegExp 匹配多个组
【发布时间】:2013-02-06 10:20:29
【问题描述】:

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

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

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

但是它只匹配第一次出现,而不匹配其余的任何(参考下面粘贴的源代码,循环只迭代一次)。

我尝试了其他一些正则表达式字符串,例如

  • (<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 匹配&lt;yt:duration ... 的所有出现?

【问题讨论】:

    标签: c++ regex youtube-api atl


    【解决方案1】:

    您将始终只有第一次(下一次)出现。要查找其他人,您需要循环使用 Match'ing,直到找不到更多匹配项。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-29
      • 2013-01-02
      • 1970-01-01
      • 2010-11-08
      • 2012-04-24
      • 1970-01-01
      • 2015-12-08
      • 2017-03-11
      相关资源
      最近更新 更多