手机版

用正则表达式(regex_replace)模拟读取INI文件

时间:2021-09-04 来源:互联网 编辑:宝哥软件园 浏览:

废话不多说,代码直接贴给大家。具体代码如下:

# include ' stdio . h ' # include s stream # include IOs stream # include fsstream # include regex使用命名空间stdvoid Trim(char * str);void LTRim(char * str);void RTRim(char * str);//测试sscanf和正则表达式//sscanf提供的这个扩展函数真的不能称为正则表达式,因为没有%//[]就写不出来字符范围,{}代表重复次数,*代表否定,*代表跳过。因此,上面url的解析可以写成这样:////char URL[]=' dv ://192 . 168 . 1 . 25:65001/1/1 '///sscanf(URL),%[:/]% *)///Explain//获取最长的字符串但不是字符串://,所以protocol=' dv \ 0//然后跳过三个字符,(%*c)实际上跳过了:////然后取一个不包括:的字符串,所以ip=192.168.1.253,这里简化一下,ip作为字符串处理,不做检查。//然后跳过冒号取端口,再跳过/取通道号。//c语言实现void test1(){ char URL[]=' dv 3360//192 . 168 . 1 ';计费协议[10];char IP[17];国际港口;int chnint类型;sscanf(url、'%[^://]%*c%*c%*c%[^:]%*c%d%*c%d%*c%d'、协议、ip、端口、chn、类型);printf('%s,%s,%d,%d,%d,%d\n ',协议、ip、端口、chn、类型);}//读取ini中的一个字符串,得到: hello world!//正常字符串1:-claim=' hello world!//正常字符串2: claim=' hello world!//正常字符串3: claim=' hello world!//正常字符串4: claim _=hello world!//干扰字符串1: cl-aim=\' hello world!//干扰字符串2: clai3m=' hello world!\ '//干扰字符串3: cla _ im=\ \' hello world!\ '//干扰字符串4: claim='' hello world!\ '//干扰字符串5: claim=@' \ n hello world!//干扰字符串6: claim=l' hello world!//未处理的1 1:声明[1]=1///未处理的1:声明[2]=1 void test2(){ charline[1000]={ 0 };char val[1000]={ 0 };char键[1000]={ 0 };FILE *fp=fopen('1.txt ',' r ');if (NULL==fp) { printf('未能打开1 . txt \ n ');返回;} while(!feof(fp)) { memset(line,0,sizeof(line));fgets(line,sizeof(line) - 1,FP);//每行包含printf ('%s ',行)\ n;修剪(线条);//提取等号前的内容memset(key,0,sizeof(key));sscanf使用的格式不是正则表达式。您不能使用\\s来表示各种空白字符,即空格或\ t,\ n,\ r,\ f s canf(line,% [\ t \ n \ r \ f=]',key);//sscanf(行,'%*[^a-zA-Z0-9_-]%[^ \t\n\r\f=]',键);printf(' key: [%s]\n ',键);//提取等号后的内容memset(val,0,sizeof(val));sscanf(线路、'%*[^=]%*c%[^\n]'、瓦尔);//不包括每行的换行符(val);printf(' val: [%s]\n ',val);//去掉两边的双引号/.//插入映射//映射[键]=值;//字符串到其他类型//atoi,ATO,atof } printf(' \ n ');fc lose(FP);}//c在上面的例子中实现了模板类t1,类T2内联t1 parse to(const T2 t){ static string stream s stream;t1 r;流t;s stream r;s stream . clear();返回r;} void test3(){ char val[1000]={ 0 };char键[1000]={ 0 };if stream fin(' 1 . txt ');字符串行;If (fin) {while (getline (fin,line))//line不包含换行符{ cout line endl///提取等号前的内容//第一组()代表任意空格字符,第二组()代表单词(with _ or-),//第三组()代表多个空格字符(or=),最后以任意字符串regex reg('([\ \ s]*)([\ \ s])//取第二组而不是原始字符串stringkey=regex _ replace (line,reg,' $ 2 ');cout ' key: { ' key ' } ' endl///提取等号后的内容//第一组()代表任意空格字符,第二组()代表单词(带_ or-),//第三组()代表多个空格字符(或=),第四组()代表任意字符,//第五组()代表任意空格字符(或回车)

reg=regex('^([\\s]*)([\\w\\-\\_])([\ \ s=])(.*)([\ \ s \ \ r \ \ n]*)$ ');//取第四组代替原串字符串val=regex_replace(行,reg,' $ 4 ');cout ' val: { ' val ' } ' endl/去除两边双引号//.//插入map//map[key]=值;//字符串转其它类型//int I=ParseToint(' 123 ');//float f=parseTofloat(' 1.23 ');//string str=ParseToString(123);} } else //没有该文件{ cout '无此类文件endl } } void main(){//test1();测试2();test3();} void lTrim(char * str) { int i,lenlen=strlen(str);for(I=0;伊琳;i ) { if (str[i]!=' str[I]!='\t' str[i]!='\n' str[i]!='\r' str[i]!='\f ')中断;} memmove(str,str i,len-I 1);返回;} void rTrim(char * str) { int i,lenlen=strlen(str);for(I=len-1;I=0;i - ) { if ((str[i]!=' ') (str[i]!=0x0a) (str[i]!=0x0d) (str[i]!='\t') (str[i]!='\f '))中断;} str[I 1]=0;返回;} void Trim(char * str) { int i,len//先去除左边的空格len=strlen(str);for(I=0;伊琳;i ) { if (str[i]!=' str[I]!='\t' str[i]!='\n' str[i]!='\r' str[i]!='\f ')中断;} memmove(str,str i,len-I 1);//再去除右边的空格len=strlen(str);for(I=len-1;I=0;i - ) { if (str[i]!=' str[I]!='\t' str[i]!='\n' str[i]!='\r' str[i]!='\f ')中断;} str[I 1]=0;返回;}/* void Trim(char * str){ ltRim(str);rTrim(str);} */

以上是使用正则表达式(regex_replace)模拟读取边肖介绍的INI文件。希望对大家有帮助。如果你有任何问题,请给我留言,边肖会及时回复你。非常感谢您对我们网站的支持!

版权声明:用正则表达式(regex_replace)模拟读取INI文件是由宝哥软件园云端程序自动收集整理而来。如果本文侵犯了你的权益,请联系本站底部QQ或者邮箱删除。