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

ExpandedBlockStart.gif 代码


/ /

 
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 
+=   " rn---------------------------------------------rn " ;
    }
   }
  }
  
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 
+=   " 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);
  
for int  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);
 }

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

 

 

 

转载于:https://www.cnblogs.com/lancidie/archive/2011/02/09/1950193.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值