Boost.Regex库使用实例

 

完整测试代码:

  1. #include <iostream>
  2. #include <boost/regex.hpp>
  3.  
  4. using namespace std;
  5. int main(int argc, char* argv[])
  6. {    //( 1 )   ((  3  )  2 )((  5 )4)(    6    )   
  7.     //(/w+)://((/w+/.)*/w+)((//w*)*)(//w+/./w+)?
  8.     //^协议://网址(x.x...x)/路径(n个/字串)/网页文件(xxx.xxx)
  9.     const char *szReg = "(//w+)://((//w+//.)*//w+)((///w*)*)(///w+//.//w+)?";
  10.     const char *szStr = "http://www.cppprog.com/2009/0112/48.html";
  11.  
  12.     {    //字符串匹配
  13.         boost::regex reg( szReg );
  14.         bool r=boost::regex_match( szStr , reg);
  15.         assert(r);
  16.     }
  17.  
  18.     {    //提取子串
  19.         boost::cmatch mat;
  20.         boost::regex reg( szReg );
  21.         bool r=boost::regex_match( szStr, mat, reg);
  22.         if(r) //如果匹配成功
  23.         {
  24.             //显示所有子串
  25.             for(boost::cmatch::iterator itr=mat.begin(); itr!=mat.end(); ++itr)
  26.             {
  27.                 //       指向子串对应首位置        指向子串对应尾位置          子串内容
  28.                 cout << itr->first-szStr << ' ' << itr->second-szStr << ' ' << *itr << endl;
  29.             }
  30.         }
  31.         //也可直接取指定位置信息
  32.         if(mat[4].matched) cout << "Path is" << mat[4] << endl;
  33.     }
  34.  
  35.     { //查找
  36.         boost::cmatch mat;
  37.         boost::regex reg( "//d+" );    //查找字符串里的数字
  38.         if(boost::regex_search(szStr, mat, reg))
  39.         {
  40.             cout << "searched:" << mat[0] << endl;
  41.         }
  42.     }
  43.  
  44.     { //替换
  45.         boost::regex reg( szReg );
  46.         string s = boost::regex_replace( string(szStr), reg, "ftp://$2$5");
  47.         cout << "ftp site:"<< s << endl;
  48.     }
  49.     { //替换2,把<>&转换成网页字符
  50.         string s1 = "(<)|(>)|(&)";
  51.         string s2 = "(?1&lt;)(?2&gt;)(?3&amp;)";
  52.         boost::regex reg( s1 );
  53.         string s = boost::regex_replace( string("cout << a&b << endl;"), reg, s2, boost::match_default | boost::format_all);
  54.         cout << "HTML:"<< s << endl;
  55.     }
  56.  
  57.     { //使用迭代器找出所有数字
  58.         boost::regex reg( "//d+" );    //查找字符串里的数字
  59.         boost::cregex_iterator itrBegin = make_regex_iterator(szStr,reg); //(szStr, szStr+strlen(szStr), reg);
  60.         boost::cregex_iterator itrEnd;
  61.         for(boost::cregex_iterator itr=itrBegin; itr!=itrEnd; ++itr)
  62.         {
  63.                 //       指向子串对应首位置        指向子串对应尾位置          子串内容
  64.                 cout << (*itr)[0].first-szStr << ' ' << (*itr)[0].second-szStr << ' ' << *itr << endl;
  65.         }
  66.     }
  67.  
  68.     { //使用迭代器拆分字符串
  69.         boost::regex reg("/");  //按/符拆分字符串
  70.         boost::cregex_token_iterator itrBegin = make_regex_token_iterator(szStr,reg,-1); //使用-1参数时拆分,使用其它数字时表示取第几个子串,可使用数组取多个串
  71.         boost::cregex_token_iterator itrEnd;
  72.         for(boost::cregex_token_iterator itr=itrBegin; itr!=itrEnd; ++itr)
  73.         {
  74.             cout << *itr << endl;
  75.         }
  76.     }
  77.  
  78.     { //使用迭代器拆分字符串2
  79.         boost::regex reg("(.)/(.)");  //取/的前一字符和后一字符(这个字符串形象貌似有点邪恶-_-)
  80.         int subs[] = {1,2};        // 第一子串和第二子串
  81.         boost::cregex_token_iterator itrBegin = make_regex_token_iterator(szStr,reg,subs); //使用-1参数时拆分,使用其它数字时表示取第几个子串,可使用数组取多个串
  82.         boost::cregex_token_iterator itrEnd;
  83.         for(boost::cregex_token_iterator itr=itrBegin; itr!=itrEnd; ++itr)
  84.         {
  85.             cout << *itr << endl;
  86.         }
  87.     }
  88.  
  89.  
  90.     cin.get();
  91.     return 0;
  92. }

转自:http://www.cppprog.com/2009/0116/53_3.html

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值