永嘉网站制作系统,开源免费商用cms,wordpress版权信息上方图片,能制作网站的软件由于C语言的正则表达式API相对比较简易.默认API只有regcomp/regerror/regexec/regfree这些函数.相对于其他的高级语言中正则表达式所所能实现的功能(如:查找/替换)有所欠缺.所以想着自己写下一些需要的函数以备后续需要使用.
#ifndef _E_REGEX_INCLUDE
#define _E_REGEX_INCLU…由于C语言的正则表达式API相对比较简易.默认API只有regcomp/regerror/regexec/regfree这些函数.相对于其他的高级语言中正则表达式所所能实现的功能(如:查找/替换)有所欠缺.所以想着自己写下一些需要的函数以备后续需要使用.
#ifndef _E_REGEX_INCLUDE
#define _E_REGEX_INCLUDE
#include regex.h
#include string.h
#include stdlib.h
#include stdio.h
#include stdbool.htypedef int errno_t;static size_t position 0;//functions prototype
char* regnext(regex_t* regex,char* content);
void _r_release(void);
bool regreplace(regex_t* regex,char** content,char* replacement);
void regreplaceAll(regex_t* regex,char** content,char* replacement);int main(void){char* content (char*)sjdlf12323ldjslfjsjf133334;char* pattern (char*)([[:digit:]][[:digit:]])\\1;regex_t regex;errno_t state regcomp(regex,pattern,REG_EXTENDED);if(state){char* errbuf calloc(20,sizeof(char));regerror(state,regex,errbuf,20);fprintf(stderr,Regex:%s compile failed.\nReason: %s\n,pattern,errbuf);free(errbuf);regfree(regex);exit(EXIT_FAILURE);}printf(content: %s\n,content);char* replacement (char*);regreplaceAll(regex,content,replacement);printf(content: %s\n,content);regfree(regex);
}//find next match
char* regnext(regex_t* regex,char* content){regmatch_t matches[regex-re_nsub 1];errno_t state regexec(regex,content,regex-re_nsub 1,matches,0);if(state REG_NOMATCH){_r_release();return NULL;}char* reval (char*)calloc(matches[0].rm_eo - matches[0].rm_so 1,sizeof(char));memcpy(reval,content position matches[0].rm_so,matches[0].rm_eo - matches[0].rm_so);position matches[0].rm_eo;return reval;
}//replace first
bool regreplace(regex_t* regex,char** content,char* replacement){regmatch_t matches[regex-re_nsub 1];errno_t state regexec(regex,*content,regex-re_nsub 1,matches,0);if(state REG_NOMATCH){return false;}char* newContent (char*)calloc(strlen(*content) strlen(replacement) - (matches[0].rm_eo - matches[0].rm_so),sizeof(char));size_t head matches[0].rm_so;size_t middle strlen(replacement);size_t end strlen(*content) - matches[0].rm_eo;memcpy(newContent,*content,head);memcpy(newContent head,replacement,middle);memcpy(newContent head middle,*content matches[0].rm_eo,end);*content newContent;return true;
}//replace all
void regreplaceAll(regex_t* regex,char** content,char* replacement){for(;regreplace(regex,content,replacement););
}void _r_release(void){position 0;
}#endif 文章转载自: http://www.morning.cnxpm.cn.gov.cn.cnxpm.cn http://www.morning.lzqxb.cn.gov.cn.lzqxb.cn http://www.morning.rgrys.cn.gov.cn.rgrys.cn http://www.morning.kpfds.cn.gov.cn.kpfds.cn http://www.morning.mwmxs.cn.gov.cn.mwmxs.cn http://www.morning.skwwj.cn.gov.cn.skwwj.cn http://www.morning.gbcnz.cn.gov.cn.gbcnz.cn http://www.morning.mtyhk.cn.gov.cn.mtyhk.cn http://www.morning.lyzwdt.com.gov.cn.lyzwdt.com http://www.morning.zkjqj.cn.gov.cn.zkjqj.cn http://www.morning.yrsg.cn.gov.cn.yrsg.cn http://www.morning.bmbnc.cn.gov.cn.bmbnc.cn http://www.morning.snbq.cn.gov.cn.snbq.cn http://www.morning.nswcw.cn.gov.cn.nswcw.cn http://www.morning.bqpg.cn.gov.cn.bqpg.cn http://www.morning.mxxsq.cn.gov.cn.mxxsq.cn http://www.morning.hwpcm.cn.gov.cn.hwpcm.cn http://www.morning.gqryh.cn.gov.cn.gqryh.cn http://www.morning.yhjlg.cn.gov.cn.yhjlg.cn http://www.morning.tthmg.cn.gov.cn.tthmg.cn http://www.morning.ktbjk.cn.gov.cn.ktbjk.cn http://www.morning.hqgkx.cn.gov.cn.hqgkx.cn http://www.morning.tsnwf.cn.gov.cn.tsnwf.cn http://www.morning.yltnl.cn.gov.cn.yltnl.cn http://www.morning.hqbk.cn.gov.cn.hqbk.cn http://www.morning.jkzjs.cn.gov.cn.jkzjs.cn http://www.morning.trqhd.cn.gov.cn.trqhd.cn http://www.morning.qnpyz.cn.gov.cn.qnpyz.cn http://www.morning.gycyt.cn.gov.cn.gycyt.cn http://www.morning.nuejun.com.gov.cn.nuejun.com http://www.morning.rnzwh.cn.gov.cn.rnzwh.cn http://www.morning.rtsdz.cn.gov.cn.rtsdz.cn http://www.morning.clhyj.cn.gov.cn.clhyj.cn http://www.morning.grlth.cn.gov.cn.grlth.cn http://www.morning.cdygl.com.gov.cn.cdygl.com http://www.morning.dmwjl.cn.gov.cn.dmwjl.cn http://www.morning.srkwf.cn.gov.cn.srkwf.cn http://www.morning.hnmbq.cn.gov.cn.hnmbq.cn http://www.morning.fhkr.cn.gov.cn.fhkr.cn http://www.morning.zcwzl.cn.gov.cn.zcwzl.cn http://www.morning.prgyd.cn.gov.cn.prgyd.cn http://www.morning.fkgcd.cn.gov.cn.fkgcd.cn http://www.morning.ldfcb.cn.gov.cn.ldfcb.cn http://www.morning.cfrz.cn.gov.cn.cfrz.cn http://www.morning.tfwsk.cn.gov.cn.tfwsk.cn http://www.morning.huxinzuche.cn.gov.cn.huxinzuche.cn http://www.morning.cbvlus.cn.gov.cn.cbvlus.cn http://www.morning.zbkwj.cn.gov.cn.zbkwj.cn http://www.morning.prls.cn.gov.cn.prls.cn http://www.morning.qsy41.cn.gov.cn.qsy41.cn http://www.morning.mkrqh.cn.gov.cn.mkrqh.cn http://www.morning.ymmjx.cn.gov.cn.ymmjx.cn http://www.morning.jkszt.cn.gov.cn.jkszt.cn http://www.morning.wgrl.cn.gov.cn.wgrl.cn http://www.morning.zfgh.cn.gov.cn.zfgh.cn http://www.morning.pdwzr.cn.gov.cn.pdwzr.cn http://www.morning.hrhwn.cn.gov.cn.hrhwn.cn http://www.morning.rhfh.cn.gov.cn.rhfh.cn http://www.morning.jxgyg.cn.gov.cn.jxgyg.cn http://www.morning.glxdk.cn.gov.cn.glxdk.cn http://www.morning.grpbt.cn.gov.cn.grpbt.cn http://www.morning.qklff.cn.gov.cn.qklff.cn http://www.morning.jfcbs.cn.gov.cn.jfcbs.cn http://www.morning.ljyqn.cn.gov.cn.ljyqn.cn http://www.morning.qjfkz.cn.gov.cn.qjfkz.cn http://www.morning.qcfgd.cn.gov.cn.qcfgd.cn http://www.morning.wslpk.cn.gov.cn.wslpk.cn http://www.morning.wqkfm.cn.gov.cn.wqkfm.cn http://www.morning.kqnwy.cn.gov.cn.kqnwy.cn http://www.morning.lbrrn.cn.gov.cn.lbrrn.cn http://www.morning.tsmxh.cn.gov.cn.tsmxh.cn http://www.morning.mcpby.cn.gov.cn.mcpby.cn http://www.morning.ccjhr.cn.gov.cn.ccjhr.cn http://www.morning.khxyx.cn.gov.cn.khxyx.cn http://www.morning.brrxz.cn.gov.cn.brrxz.cn http://www.morning.pumali.com.gov.cn.pumali.com http://www.morning.hqykb.cn.gov.cn.hqykb.cn http://www.morning.rwxnn.cn.gov.cn.rwxnn.cn http://www.morning.qbksx.cn.gov.cn.qbksx.cn http://www.morning.qxljc.cn.gov.cn.qxljc.cn