vc6.0下使用Boost正则表达式库

要使用Boost有两种方法,这是我认为最简单的一种,也是最详细的。

1.到http://www.boost.org/users/history/下载安装包,这里下载的是Version 1.38.0,高版本的在我这里编译不成功~~~~

2.将VC安装路径C:\Program Files\Microsoft Visual Studio\VC98\Bin\下的VCVARS32.BAT复制一份到D:\Program Files\boost_1_38_0\boost_1_38_0\libs\regex\build\

3.进入cmd并运行:

>>VCVARS32.BAT //这时屏幕显示 Setting environment for using Microsoft Visual C++ tools.

>>nmake vc6.mak  (如果失败则尝试nmake -f vc6.mak)//这个步骤需要耐心等待...

完成后会生成vc6目录,D:\Program Files\boost_1_38_0\boost_1_38_0\libs\regex\build\vc6\此目录下有2个DLL文件和多个LIB文件。

4.修改VC设置:Tools->Options->Directories->Include files下添加上面的安装路径D:\Program Files\boost_1_38_0\boost_1_38_0

Tools->Options->Directories->Library files下添加LIB文件路径D:\Program Files\boost_1_38_0\boost_1_38_0\libs\regex\build\vc6

5.测试以下代码,可正常编译链接通过,生成EXE文件。

编译以下代码的时候会提示:fatal error C1010: unexpected end of file while looking for precompiled head

解决方法:http://zhidao.baidu.com/question/236360935.html 我使用第二种:#include"stdafx.h"

例子一:

#include <cstdlib>
#include <stdlib.h>
#include <boost/regex.hpp>
#include <string>
#include <iostream>

using namespace std;
using namespace boost;

regex expression("^select ([a-zA-Z]*) from ([a-zA-Z]*)");

int main(int argc, char* argv[])
{
 std::string in;
 cmatch what;
 cout << "enter test string" << endl;
 getline(cin,in);
 if(regex_match(in.c_str(), what, expression))
 {
  for(int i=0;i<what.size();i++)
   cout<<"str :"<<what.str()<<endl;
 }
 else
 {
  cout<<"Error Input"<<endl;
 }
 return 0;
}

 

例子二:

#include <boost/regex.hpp>
bool validate_card_format(char* pstr)
{
 static const boost::regex e("(}");
 return boost::regex_match(pstr,e);
}

int main()
{
 char *pstr[4] = {"0000111122223333","0000 1111 22222 3333","0000-1111-2222-3333","000111122223333"};
 for (int i = 0; i < 4; i++)
 {
  printf("%s  %d\r\n",pstr[i],validate_card_format(pstr[i]));
 }
 return 0;
}

转自:http://hi.baidu.com/cannotforget/item/048749abb76bd0f715329bc8

posted @ 2013-02-18 12:02  stma  阅读(335)  评论(0)    收藏  举报