当前位置: 首页 > news >正文

C语言正则表达式 regnext regreplace regreplaceAll

由于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.h>typedef 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.\n""Reason: %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.lryc.cn/news/334100.html

相关文章:

  • 使用aspose相关包将excel转成pdf 并导出
  • 按关键字搜索商品API接口搜索关键字,显示商品总数,标题,图片,优惠价参数等
  • 网络基础知识入门
  • D435i发布的话题学习
  • Springboot启动过程
  • 网络安全之命令注入
  • 使用GDAL进行简单的坐标系转换
  • 【AIGC调研系列】AI大模型结合迁移学习进行微调的应用
  • 低代码革新:软件开发的未来潜力与创新路径探索
  • AI智能校色解决方案,专业级画质提升
  • 面试算法-148-轮转数组
  • Linux——静态库 共享库
  • 基于Spring Boot+Vue的在线拍卖系统
  • Unity构建详解(6)——SBP的Bundle写操作生成
  • 2024新版PHP在线客服系统多商户AI智能在线客服系统源码机器人自动回复即时通讯聊天系统源码PC+H5
  • 使用GPT需要注意的事项
  • 学习人工智能:为何PyTorch深度学习框架不可或缺
  • Python基于Tkinter的加法游戏
  • 基于Springboot4S店车辆管理系统
  • 深入浅出 -- 系统架构之分布式架构
  • 6. Z 字形变换(Java)
  • 【Linux入门】用户的基本指令
  • 3.9 Python格式化字符串
  • Linux驱动学习:从Linux主机nfs共享文件到uboot
  • Linux下场景模拟--cpu、内存打满测试
  • Vue中watch与计算属性computed
  • nginx部署前端教程
  • 设计模式:工厂模式
  • 系统监测工具-tcpdump的使用
  • Java智慧校园系统源码 微信小程序+电子班牌