boost 正则
参考地址:http://www.cnblogs.com/wubiyu/archive/2008/11/30/1344093.html
常用的类
boost::regex 正则表达式
boost::cmatch 以char数组为容器,存储匹配返回值。
boost::smatch 以std::string为容器,存储匹配返回值。
boost::regex_match 匹配算法
boost::regex_search 查找算法
boost::regex_replace 替换算法
/*
* =====================================================================================
*
* Filename: reg2.cc
*
* Description:
*
* Version: 1.0
* Created: 08/27/2011 09:21:07 PM
* Revision: none
* Compiler: gcc
*
* Author: kangle.wang (mn), wangkangluo1@gmail.com
* Company: APE-TECH
*
* =====================================================================================
*/
#include <cstdlib>
#include <stdlib.h>
#include <boost/regex.hpp>
#include <string>
#include <iostream>
usingnamespace std;
usingnamespace boost;
regex expression("^select ([a-zA-Z]*) from ([a-zA-Z]*)");
int main(int argc, char* argv[])
{
std::stringin;
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;
}
return0;
}
编译:
g++ -g -Wall -O0 reg2.cc -o reg2 -lboost_regex
output*******
enter test string
select name from table
str :select name from table
完