Regular Expression之CAtlRegExp

来源:互联网 发布:淘宝嘉年华有什么折扣 编辑:程序博客网 时间:2024/06/04 19:32

bool matchRegExp(const CString& strTag, const CString& strPattern, RegExpResults& rer)
 {
  rer.clear();
  bool bRet = false;
  CAtlRegExp<> regex;
  REParseError status = regex.Parse(strPattern);
  if (REPARSE_ERROR_OK == status)
  {
   CAtlREMatchContext<> mc;
   const CAtlRegExp<>::RECHAR* pCon = strTag;
   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;
     wchar_t *strMatch = new wchar_t[nLength+1];
     strMatch[nLength] = 0;
     _tcsncpy_s(strMatch, nLength+1, 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;
 } 

原创粉丝点击