<div id=\"TextTitle\"><span class=\"newstitle\">hello ,world</span></div>我想提取上面这句话中间的<span class=\"newstitle\">hello ,world</span>,想使用正则表达式,利用VC.net自己提供的CAtlRegEx完成,希望有达人帮我,对这个类我不是很熟,试验了很久总也提取不出来,谢谢了

解决方案 »

  1.   

    正则式可能是这样,但我对CAtlRegEx使用不是很熟,帮忙看下下面代码好吗
        CAtlRegExp<> reContent;
        // Five match groups: scheme, authority, path, query, fragment
        REParseError status = reContent.Parse(
            L"<span.*span>" );    if (REPARSE_ERROR_OK != status)
        {
            // Unexpected error.
            return;
        }    CAtlREMatchContext<> mcUrl;
        if (!reContent.Match(
    L"<div id=\"TextTitle\"><span class=\"newstitle\">hello, world</span></div>",
            &mcUrl))
        {
            // Unexpected error.
            return;
        }
    wchar_t l[200];
    CString str;
        for (UINT nGroupIndex = 0; nGroupIndex < mcUrl.m_uNumGroups;
             ++nGroupIndex)
        {
            const CAtlREMatchContext<>::RECHAR* szStart = 0;
            const CAtlREMatchContext<>::RECHAR* szEnd = 0;
            reContent.GetMatch(nGroupIndex, &szStart, &szEnd);        ptrdiff_t nLength = szEnd - szStart;
    //str.Append(CStr(
            swprintf_s(l,L"%d: \"%.*s\"\n", nGroupIndex, nLength, szStart);
    str.Append(CString(l));
        }   // return; MessageBox(str);
    最后显示出来的总是空,不解
      

  2.   

    估计微软这个有问题,我以前写的时候用的是boost的regex库,而且一楼的语法是正确的,但是无法应对嵌套的。比如:
    <span><div id=\"TextTitle\"> <span class=\"newstitle\">hello ,world </span> </div></span>