现在的位置: 首页 > 综合 > 正文

ATL中使用正则表达式

2013年07月01日 ⁄ 综合 ⁄ 共 1353字 ⁄ 字号 评论关闭

将ATL进行了一次封装,不用每次都写繁琐的调用

 

bool matchRegExp(const CString& strTag, const CString& strPattern,std::vector<vector<CString>> & rer)
{
    rer.clear();
    
bool bRet = false;
    ATL::CAtlRegExp
<> regex;
    ATL::REParseError status 
= regex.Parse(strPattern);
    
if (REPARSE_ERROR_OK == status)
    
{
        CAtlREMatchContext
<> mc;
        
const CAtlRegExp<>::RECHAR* pCon = strTag;
        std::vector
<CString> record;
        
while(regex.Match(pCon, &mc, &pCon))
        

            record.clear();
            record.reserve(mc.m_uNumGroups);
            
for (UINT nGroupIndex = 0; nGroupIndex < mc.m_uNumGroups;
                
++nGroupIndex)
            
{
                
const CAtlREMatchContext<>::RECHAR* szStart = 0;
                
const CAtlREMatchContext<>::RECHAR* szEnd = 0;
                mc.GetMatch(nGroupIndex, 
&szStart, &szEnd);

                ptrdiff_t nLength 
= szEnd - szStart;
                TCHAR 
*strMatch = new TCHAR[nLength+1];
                strMatch[nLength] 
= 0;
                _tcsncpy(strMatch, szStart, nLength);

                record.push_back(strMatch);
                delete[] strMatch;
            }

            rer.push_back(record);
            
if(pCon-strTag >= strTag.GetLength())//seems there is a bug in catlregexp library, i have to check this by myself.
                break;
        }

    }

    
return rer.size() != 0;
}
 

 

抱歉!评论已关闭.