VC6下正则greta库的测试和使用手记

发表于:2009-5-22 12:14

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:gisZhang    来源:cnblogs

//////////////////////////////////

 if( nChar==VK_ESCAPE )
  CDialog::OnOK();
 else if( nChar==VK_F5 ) //匹配查找
 {
  UpdateData();
  m_strResult = "";
  match_results result;
  REGEX_FLAGS dw = GLOBAL | ALLBACKREFS;
  if( m_bCase ) dw |= NOCASE;
  if( m_bMulti ) dw |= MULTILINE;
  if( m_bSingle ) dw |= SINGLELINE;
  //
  double tmS = clock();
  //
  rpattern pat((LPCTSTR)m_strReg, dw);
  int iGroups = pat.cgroups();
  int nCount = 0;
  match_results::backref_type br = pat.match( (LPCTSTR)m_strSource, result );
  if( 0 )//遍历结果方式1,任选一种方式即可
  {
   match_results::backref_vector vec = result.all_backrefs();
   match_results::backref_vector::iterator iter;
   if( br.matched )
   {
    for( iter = vec.begin(); iter != vec.end(); iter++ )
    {
     nCount++;
     string str = (*iter).str();
     m_strResult += str.c_str();
     m_strResult += "\r\n---------------------------------------------\r\n";
    }
   }
  }
  if( 1 )//遍历结果方式2
  {
   if( br.matched )
   {
    for( int i=0;i<result.cbackrefs();i++ )
    {
     if( i%iGroups == 0 )
     {
      nCount++;
      m_strResult += result.backref(i).str().c_str();
      m_strResult += "\r\n---------------------------------------------\r\n";
     }
    }
   }
  }
  double tmE = clock();
  CString strTip;
  strTip.Format(_T("  运行时间 %.2fms, 共找到 %d个匹配;"), double(tmE-tmS), nCount);
  GetDlgItem(IDC_STATIC_TIP)->SetWindowText(strTip);
  //
  UpdateData(FALSE);
 }
 else if( nChar == VK_F6 )//替换
 {
  UpdateData();
  m_strResult = "";
  //
  REGEX_FLAGS dw = GLOBAL | ALLBACKREFS;
  if( m_bCase ) dw |= NOCASE;
  if( m_bMulti ) dw |= MULTILINE;
  if( m_bSingle ) dw |= SINGLELINE;
  double tmS = clock();
  //
  rpattern pat((LPCTSTR)m_strReg, (LPCTSTR)m_strSub, dw);
  subst_results subResult;
  //
  string str((LPCTSTR)m_strSource);
  int nCount = pat.substitute(str, subResult);
  m_strResult = str.c_str();
  //
  double tmE = clock();
  CString strTip;
  strTip.Format(_T("  运行时间 %.2fms, 共完成替换 %d处;"), double(tmE-tmS), nCount);
  GetDlgItem(IDC_STATIC_TIP)->SetWindowText(strTip);
  //
  UpdateData(FALSE);
 }
 else if( nChar == VK_F7 )//分割字符串
 {
  UpdateData();
  m_strResult = "";
  //
  REGEX_FLAGS dw = GLOBAL | ALLBACKREFS;
  if( m_bCase ) dw |= NOCASE;
  if( m_bMulti ) dw |= MULTILINE;
  if( m_bSingle ) dw |= SINGLELINE;
  double tmS = clock();
  //
  rpattern pat((LPCTSTR)m_strReg, dw);
  split_results splitResult;
  //
  string str((LPCTSTR)m_strSource);
  int nCount = pat.split(str, splitResult);
  for( int ni=0;ni<nCount;ni++ )
  {
   string strSplit = splitResult[ni];
   m_strResult += strSplit.c_str();
   m_strResult += "\r\n---------------------------------------------\r\n";
  }
  //
  double tmE = clock();
  CString strTip;
  strTip.Format(_T("  运行时间 %.2fms, 共找到 %d个匹配;"), double(tmE-tmS), nCount);
  GetDlgItem(IDC_STATIC_TIP)->SetWindowText(strTip);
  //
  UpdateData(FALSE);
 }

/////----------结束-----------////////

该工程项目代码在这里下载,已经包含了greta2.6.4源码文件;

以上为初步测试结果,可能在使用和介绍中有错误和不当之初,欢迎大家多交流探讨。

22/2<12
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号