GUN regex正则表达式的使用

来源:互联网 发布:python爬取天气数据 编辑:程序博客网 时间:2024/06/01 03:58

自己写了个简单的正则替换程序,使用GUN regex库,VC下编译通过

库文件可以去这里下载:http://download.csdn.net/detail/nsdcoder/9573050

互相学习,资源免费


#include <string>#include "regex.h"#pragma comment(lib, "regex.lib")void regtest(std::string regstr, std::string src, std::string newstr);int main(int argc, char *argv){std::string reg = "&[^;]\\{2,6\\};";//&[^;]{2,6}; <[^>]*[p|P]>std::string src = "<a class=123>&quot ;</a>&ql;<p>djkslf</p>";regtest(reg, src, "---");return 0;}void regtest(std::string regstr, std::string src, std::string newstr){regex_t oRegex;int nErrCode = 0;char szErrMsg[1024];size_t unErrMsgLen = 0;if((nErrCode = regcomp(&oRegex, regstr.c_str(), 0)) == 0){int nmatch = 10;regmatch_t what[10];while((nErrCode = regexec(&oRegex, src.c_str(), nmatch, what, 0)) == 0){printf("Match Result: %s \n", src.substr(what[0].rm_so, what[0].rm_eo-what[0].rm_so).c_str());src = src.substr(0, what[0].rm_so) + newstr + src.substr(what[0].rm_eo, src.length()-what[0].rm_eo);}regfree(&oRegex);}else{unErrMsgLen = regerror(nErrCode, &oRegex, szErrMsg, sizeof(szErrMsg));unErrMsgLen = unErrMsgLen < sizeof(szErrMsg) ? unErrMsgLen : sizeof(szErrMsg) - 1;szErrMsg[unErrMsgLen] = '\0';printf("ErrMsg: %s\n", szErrMsg);regfree(&oRegex);}printf("src: %s\n", src.c_str());}


1 0
原创粉丝点击