我是靠谱客的博主 清脆手机,这篇文章主要介绍C regex.h,现在分享给大家,希望可以做个参考。

C也是存在正则表达式的

Linux下regex.h知识点和使用样例

上文中有一个样例代码,进行了测试

总结一下有些注意点:

1.上述代码的匹配子串很奇怪,为什么会出现

cnt=6
a very
cnt=1
a
cnt=4
very
cyc:**************3

的结果??

2.可以使用^xxxx$来限定字符串从开头到结尾都要匹配

3.REG_NEWLINE的效果没试出来

 

现下想实现判断整串字符串都由数字和字母组成。

char *haa = "dasdwer213%1fsw";
char *regex = "^[0-9a-zA-Z]+$";
struct timeval s1, s2;
gettimeofday(&s1, NULL);
regex_t comment;
regcomp(&comment, regex, REG_EXTENDED | REG_NEWLINE | REG_NOSUB);
//
regmatch_t regmatch[100]; //如果不保存结果,那么不必申请这个
int j = regexec(&comment, haa, 0,
NULL, 0);
printf("Get Coden");
if (j == REG_NOERROR) {
printf("Successn");
} else if (j == REG_NOMATCH) {
printf("Failedn");
} else {
size_t len = regerror(j, &comment, NULL, 0);
//
printf("Error Len :%dn",len);
char buf[len];
bzero(buf, len);
regerror(j, NULL, buf, len);
printf("Error :%sn", buf);
}
regfree(&comment);
gettimeofday(&s2, NULL);
int time = (s2.tv_sec - s1.tv_sec) * 1000000 + (s2.tv_usec - s1.tv_usec);
printf("time :%dn", time);
return 0;

 

转载于:https://www.cnblogs.com/Jacket-K/p/9259575.html

最后

以上就是清脆手机最近收集整理的关于C regex.h的全部内容,更多相关C内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(127)

评论列表共有 0 条评论

立即
投稿
返回
顶部