龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > VC开发 >

VC6.0下配置boost库使用正则表达式(2)

时间:2009-12-30 15:42来源:未知 作者:admin 点击:
分享到:
4、编写程序测试 SDK下的测试: #include "stdafx.h" #include cstdlib #include stdlib.h #include boost/regex.hpp #include string #include iostream using namespace std; using namespace boost;

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.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.str()).c_str();//string转CString
    MessageBox(sRet);
   }
}
else
{
   MessageBox("Error Input");
}

输出的结果跟上面一样。

精彩图集

赞助商链接