boost用法【转】

/ 示例1
#include 
<string> 
#include 
<iostream> 
#include 
<boost/regex.hpp> 

int main() { 
      boost::regex reg(
"\\d+"); 
      boost::smatch m; 
      std::
string str("my phone is 88886666 and age is 25"); 
      std::
string::const_iterator it = str.begin(); 
      std::
string::const_iterator end = str.end(); 
    
while(boost::regex_search(it, end, m, reg)) { 
          std::cout 
<< m.str() << std::endl; 
          it 
= m[0].second; 
      } 
      boost::regex my_reg(
"My", boost::regex::icase | boost::regex::perl); 
      std::
string res = boost::regex_replace(str, my_reg, "Solee's"); 
      std::cout 
<< res << std::endl; 
    
return 0
}

// 示例2
// 使用迭代器获取所有匹配
#include <boost/regex.hpp> 
#include 
<iostream> 
#include 
<string> 
#include 
<algorithm> 
using namespace std; 

class Show { 
public:     
    template
<class T> 
    
void operator()(const T& t) 
         cout 
<< t.str() << endl; 
     } 
}; 

int main(int argc, char* argv[]) 

     boost::regex reg(
"\\d+"); 
     
string str("my num is 21314 and age is 26."); 
     boost::sregex_iterator it(str.begin(), str.end(), reg); 
     boost::sregex_iterator end; 
     for_each(it, end, Show()); 
    
return 0
}
posted on 2008-12-25 09:48  风荷小筑  阅读(657)  评论(0编辑  收藏  举报