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 Code\n");
    if (j == REG_NOERROR) {
        printf("Success\n");
    } else if (j == REG_NOMATCH) {
        printf("Failed\n");
    } else {
        size_t len = regerror(j, &comment, NULL, 0);
//        printf("Error Len :%d\n",len);
        char buf[len];
        bzero(buf, len);
        regerror(j, NULL, buf, len);
        printf("Error :%s\n", buf);
    }
    regfree(&comment);
    gettimeofday(&s2, NULL);
    int time = (s2.tv_sec - s1.tv_sec) * 1000000 + (s2.tv_usec - s1.tv_usec);
    printf("time :%d\n", time);
    return 0;

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值