Boost正则表达式库regex常用search和match示例 - 编程语言 - 开发者第2241727个问答
Boost正则表达式库regex常用search和match示例
发表回复
示例很简单,但是很有针对性,可以根据示例进行不用的修改,之后加入到各种工程中。
#include <cstdlib> #include <stdlib.h> #include <boost/regex.hpp> #include <string> #include <iostream> using namespace std; using namespace boost; regex subexp("e[cl][oe][mc]"); regex expression("^select ([a-zA-Z]*) from ([a-zA-Z]*) secom"); int main(int argc, char* argv[]) { //输入:select name from table secom //输出:str:select name from table //str:name //str:table //std::string in; cmatch what; string in="select name from table secom" ; cmatch sub ; if(regex_match(in.c_str(),what,expression)) { //regex_match : 是对整个输入块的匹配,整个块如不匹配则不能成功 for(unsigned int i=0;i<what.size();i++) cout<<"str :"<<what[i].str()<<endl; } else { cout<<"Error Match"<<endl; } printf("%s ",in.c_str()); while(regex_search(in.c_str(),sub,subexp)) { //单字搜索,将每次匹配到的结果输出 printf("%s ",sub.base()); printf("%s ",sub[0].str().c_str()); in = sub[0].second; } return 0 ; }