boost库

来源:互联网 发布:刘宇昆 爱的算法pdf 编辑:程序博客网 时间:2024/04/26 01:08

vc++6.0环境下的使用正则表达式的一些总结

1,如何下载boost库?

官方下载地址http://www.boost.org/,这里我选择下载boost_1_34_0.zip

2,如何编译boost库中的正则表达式的子库?

如果要全编译boost得花一两个小时,如果只选择编译boost中的正则表达式的子库,大概10分钟,遵照以下几个步骤可以编译boost中的正则表达式的子库:

    a) 开始 / 运行 / cmd

    b) 输入 / Microsoft Visual Studio / VC98 / Bin / VCVARS32.BAT,回车

    c) cd C:/boost_1_34_0/libs/regex/build   // 转到boost中的正则表达式的子库的目录

    d) 输入 nmake -fvc6.mak, 回车

经过以上步骤就能成功编译boost中的正则表达式的子库

3,如何设置vc6?

打开vc6,菜单 tools / options,在选项框内选择directories标签页

a) 在Show directories for 下选择 include files, 在Directories:下添加一个新的目录C:/BOOST_1_34_0

b) 在Show directories for 下选择 library files, 在Directories:下添加一个新的目录C:/boost_1_34_0/libs/regex/build/vc6

经过以上两步设置,就可以进行测试了,以下给出了示例

4,示例

// 示例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
}


// 示例3
// 实现一个字符串分割函数split.
#include 
<string> 
#include 
<iostream> 
#include 
<vector> 
#include 
<boost/regex.hpp> 
#include 
<boost/shared_ptr.hpp> 

using namespace std; 

template
<class Tchar> 
boost::shared_ptr
< vector<basic_string<Tchar> > > split(const basic_string<Tchar>& t, 
                                                        
const basic_string<Tchar>& tokenStr) 
     boost::shared_ptr
< vector<basic_string<Tchar> > > pRtn =  
         boost::shared_ptr
< vector<basic_string<Tchar> > >(new vector<basic_string<Tchar> >); 
    
static const boost::basic_regex<Tchar, boost::regex_traits<Tchar> > reg(tokenStr); 
     boost::regex_token_iterator
<basic_string<Tchar>::const_iterator >  
         begin(t.begin(), t.end(), reg, 
-1); 
     boost::regex_token_iterator
<basic_string<Tchar>::const_iterator > end; 
    
while(begin != end) 
         pRtn
->push_back(*begin++); 
     }
 
     
    
return pRtn; 
}
 


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

     
string str("1,2,3,45,7"); 
     
string token(","); 
     boost::shared_ptr
< vector<string> > pVec = split(str, token); 
    
for(vector<string>::const_iterator it = pVec->begin();  
                             it 
!= pVec->end(); ++it) 
         cout 
<< *it << endl; 
     }
 
    
return 0
}
 
原创粉丝点击