zoukankan      html  css  js  c++  java
  • greta简单使用

     

    GRETA 是 Microsoft Research 的 Eric Niebler 开发的一个 free C++ 正则表达式实现,下载地址 http://research.microsoft.com/projects/greta/ 。 greta主要有如下类:

     rpattern 正则表达式类。

    match_results 执行结果类

    rpattern的主要方法:

    rpattern 构造函数。设置正则表达式和参数。
    match 执行正则表达式。可以接受三种参数:std::string, const char*, const_iterator。返回值为match_results::backref_type。
    subsitute 替换。只能接受 std::string。返回值为替换的数目。
    count 计算正则表达式在串中出现的次数。可以接受和 match 函数一样的三种参数。
    split 用正则表达式作为分隔符来切分串。也可以接受和 match 一样的三种参数。
    cgroups 计算正则表达式中包含的组数目。至少为1。
    match_results的主要方法:这些方法都只有在调用过 rpattern::match 后才能使用。
    all_backref 返回 match_results 中的所有 backref 数组。包括组。如果在rpattern 中没有指定 GLOBAL | ALLBACKREFS 参数,则 all_backref 中最多只会包含 rpattern.cgroups() 个元素;若指定,会包含 rpattern.count() * rpattern.cgroups() 个元素。
    backref 返回 match_results 中指定位置的 backref。包括组。
    cbackrefs 返回 match_results 中 backref 个数。包括组。
    rpatter 初始化时的参数说明:
    NOCASE 不区分大小写。
    GLOBAL 全局。如果指定该参数,rpattern::subsitute 会将串中全部匹配表达式的参数替换;否则(默认),若指定了 RIGHTMOST 参数,替换最后一个,没有指定(默认),替换第一个。
    MULTILINE 若不指定(默认),'^' 匹配串的开头,'$' 匹配串的结束;若指定,^ 匹配行开头,$ 匹配行结束。
    SINGLELINE 若不指定(默认),'.' 匹配除换行符(\n)外的任何字符;若指定,'.'也匹配换行符。SINGLELINE 看起来和 MULTILINE 不可一起用,其实她们的含义不是矛盾的,可以一起用。这是 GRETA 的 sb 之处,不知道是否从 perl copy过来的。
    RIGHTMOST 查找最右边的、最长的匹配串。默认是找左边的、最长的匹配串。btw, 在正则表达式中加 ? 可以使表达式找最短的匹配串。如串"test",re ".+" 会匹配整个 "test" 串,而 re ".+?" 则只匹配 "t"。
    NOBACKREFS 不记录 backref 。替换时用上此参数,可大幅度提高速度。这是文档上说的,俺没有试过。
    ALLBACKREFS 参见 match_results::all_backref 的解释。
    上面说了 greta 的主要类。下面是如何使用 greta 。 使用 greta ,只要包含 regexpr2.h 这个头文件即可。(编译不成功时包含GRETA包中的两个CPP文件再编译试试)

    ==============================================

    sample : 匹配 (from greta user's guide)。

     match_results results;

    string str( "The book cost $12.34" );

    rpattern pat( "http://www.cnblogs.com/lsmdiao0812/admin/file://$(//d+)(//.(//d//d))?" );

    // Match a dollar sign followed by one or more digits,

    // optionally followed by a period and two more digits.

     // The double-escapes are necessary to satisfy the compiler.

    match_results::backref_type br = pat.match( str, results );

     if( br.matched )

    {

        cout << "match success!" << endl;

        cout << "price: " << br << endl;

    }

    else

    {

        cout << "match failed!" << endl;

    }

    结果为: match success! price: $12.34

    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/sunxysong/archive/2005/05/23/378778.aspx

    代码


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

     
    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 );
      
    if0 )//遍历结果方式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 
    += "rn---------------------------------------------rn";
        }
       }
      }
      
    if1 )//遍历结果方式2
      {
       
    if( br.matched )
       {
        
    forint i=0;i<result.cbackrefs();i++ )
        {
         
    if( i%iGroups == 0 )
         {
          nCount
    ++;
          m_strResult 
    += result.backref(i).str().c_str();
          m_strResult 
    += "rn---------------------------------------------rn";
         }
        }
       }
      }
      
    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);
      
    forint ni=0;ni<nCount;ni++ )
      {
       
    string strSplit = splitResult[ni];
       m_strResult 
    += strSplit.c_str();
       m_strResult 
    += "rn---------------------------------------------rn";
      }
      
    //
      double tmE = clock();
      CString strTip;
      strTip.Format(_T(
    "  运行时间 %.2fms, 共找到 %d个匹配;"), double(tmE-tmS), nCount);
      GetDlgItem(IDC_STATIC_TIP)
    ->SetWindowText(strTip);
      
    //
      UpdateData(FALSE);
     }

    /////----------结束-----------////////
  • 相关阅读:
    别用言语考验底线
    QQ登陆接口
    cookie设置今日不提醒功能
    Sublime Text 3常用插件
    浏览器的标准模式与怪异模式的设置与区分方法
    JS中offsetTop、clientTop、scrollTop、offsetTop各属性介绍
    javascript中闭包的工作原理
    javascript判断图片是否加载完成方法整理
    社会十二效应法则
    小故事
  • 原文地址:https://www.cnblogs.com/lancidie/p/1950193.html
Copyright © 2011-2022 走看看