当前位置:资源 > >正文
【世界快播报】2 个小例子了解 C 语言使用正则表达式
2023-04-20 14:30:48    互联网


(相关资料图)

哈喽,我是老吴。我的日常工作基本离不开正则表达式,不过一般是在 Shell 脚本或者命令行里使用。今天抽空了解了一下 C 语言下的使用方式,马上分享给大家。如果你还不了解正则表达式,可以先瞅一眼 《Linux Shell 脚本攻略》第 4.2 章节正则表达式入门。

五分钟就可以入门,下面来看第一个例子。

#includestaticconstchar*conststr="1)JohnDriverhacker;n2)JohnDoe;n3)JohnFoo;n";staticconstchar*constre="John.*o";intmain(void){staticconstchar*s=str;regex_tregex;regmatch_tpmatch[1];regoff_toff,len;if(regcomp(®ex,re,REG_NEWLINE))exit(EXIT_FAILURE);printf("Matches:n");for(inti=0;;i++){if(regexec(®ex,s,ARRAY_SIZE(pmatch),pmatch,0))break;off=pmatch[0].rm_so+(s-str);len=pmatch[0].rm_eo-pmatch[0].rm_so;printf("#%d:n",i);printf("offset=%jd;length=%jdn",(intmax_t)off,(intmax_t)len);printf("substring="%.*s"n",len,s+pmatch[0].rm_so);s+=pmatch[0].rm_eo;}}

运行结果:

$./exampleMatches:#0:offset=25;length=7substring="JohnDo"#1:offset=38;length=8substring="JohnFoo"

成功匹配出符合 "John.*o" 表达式的两个字符串。一般正则表达式相关的接口就是 2 个:1、编译,对应 regcomp();2、查找匹配项,对应 regexec();匹配到的结果会保存在结构体 regmatch_t 里,注意,这个结构体里只保存了匹配项的 start offset 和 end offset。

typedefstruct{regoff_trm_so;regoff_trm_eo;}regmatch_t;

如果你的系统不是 Linux,而是嵌入式 RTOS 或者裸机程序,可以使用这个开源的正则表达式库:

https://github.com/kokke/tiny-regex-c

tiny-regex-c 是一个有 1K star 的超小巧 C 语言 regex 库。源码就 2 个文件:re.c 和 re.h,超容易移植。用法看这个例子:

#include"re.h"intmain(void){/*Standardinttoholdlengthofmatch*/intmatch_length;/*Standardnull-terminatedC-stringtosearch:*/constchar*string_to_search="ahem.."helloworld!"..";/*Compileasimpleregularexpressionusingcharacterclasses,meta-charandgreedy+non-greedyquantifiers:*/re_tpattern=re_compile("[Hh]ello[Ww]orld\s*[!]?");/*Checkiftheregexmatchesthetext:*/intmatch_idx=re_matchp(pattern,string_to_search,&match_length);if(match_idx!=-1){printf("match:%sn",string_to_search+match_idx);}}

运行结果:

match:helloworld!"..

tiny-regex-c 的 接口和 第一个例子 的posix regex 类似,都是一目了然,就不再赘述了。

—— The End ——

X 关闭

往期话题
最近更新

Copyright ©  2015-2022 热讯产业园区网版权所有  备案号:豫ICP备20005723号-6   联系邮箱:29 59 11 57 8@qq.com