支持正则的库 大概有以下几种:boost,GNU,VC7 带的 ATL 中的和微软发布的 greta。 网上的朋友对 boost 评价不错。
1、 下载 boost 库, 解压的目录是 D:\boost
2、 编译,因为只要支持正则表达式就可以,因此只需要编译 D:\boost\libs\regex\build 目录下的文件。
在“运行”里输入“cmd”,打开命令提示符
输入命令“cd D:\boost\libs\regex\build ”//设置当前目录
输入命令“D:”//转到当前目录
visual studio 安装目录下的 VC98\Bin\VCVARS32.BAT 是配置环境变量的批处理文件,但是配置的参数只在 当前窗口有效
把 VCVARS32.BAT 文件用鼠标拖到打开的 cmd 窗 口 ("D:\Program Files\Microsoft Visual Studio\VC98\Bin\VCVARS32.BAT") , 然后回车。 输入命令“nmake -f vc6.mak”等待几分钟
多了一个 vc6 的文件夹,里面是很多 lib 和 dll 文件
3、 配置 VC6.0,使 它编译 使时候能找到正则库
把 vc6 下的所有 lib 和 dll 文件拷贝到 Visual Studio 安装目录下的 VC98\boostRegx(boostRegx 是我自己建的)
打开 vc6.0,选择 “Tools->Options->Directories->Include files”,加入一行“D:\BOOST”
选 择“Tools->Options->Directories->Library file”,加入一行“C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO\VC98\BOOSTREX”
4、 编写程序测试
//SDK 下的测试:
#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[i].str()<<endl;
} else
{
cout<<"Error Input"<<endl;
}
return 0;
}
输入: select name from table
输出:
str:select name from table
str:name
str:table
//MFC 下的测试(有几个地方要注意,下面有提示):
//新建一个对话框的 MFC 工程,加入头文件 #include <boost/regex.hpp>
//在按钮鼠标单击事件响应函数中加入
boost::regex expression("^select ([a-zA-Z]*) from ([a-zA-Z]*)");
CString in = "select gm from tab";
CString sRet;
boost::cmatch what;
if(boost::regex_match(LPCSTR(in), what, expression))//CString 转 string
{
for(int i=0;i<what.size();i++)
{
sRet = (what[i].str()).c_str(); //string 转 CString
MessageBox(sRet);
}
} else
{
MessageBox("Error Input");
}