c正则匹配小计

参考地址:

ibm手册http://publib.boulder.ibm.com/infocenter/iseries/v7r1m0/index.jsp?topic=%2Frtref%2Fregcomp.htm

书籍http://book.douban.com/subject/3794240/

csdn阅读http://blog.csdn.net/wwwljc/article/details/6159644

c++11正则http://www.cnblogs.com/yejianfei/archive/2012/10/07/2713715.html

几个正则表达式(邮箱email,ipv4)匹配:

#include <sys/types.h>
#include <regex.h>
#include <stdio.h>

int main(int argc, char *argv[]){
        regex_t regex;
        int reti;
        char msgbuf[100];
const char *reg_exp = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
//ipv4 '^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\\.){3}(25[0-5]|2[0-4][0-9]|1[09][0-9]|[1-9][0-9]|[0-9])$'
/* Compile regular expression */ reti = regcomp(&regex, reg_exp, REG_EXTENDED); if( reti ){ fprintf(stderr, "Could not compile regex\n"); exit(1); } /* Execute regular expression */ reti = regexec(&regex, "wangkangluo1@136.com", 0, NULL, 0); if( !reti ){ puts("Match"); } else if( reti == REG_NOMATCH ){ puts("No match"); } else{ regerror(reti, &regex, msgbuf, sizeof(msgbuf)); fprintf(stderr, "Regex match failed: %s\n", msgbuf); exit(1); } /* Free compiled regular expression if you want to use the regex_t again */ regfree(&regex); return 0; }

 

posted @ 2012-11-25 23:46  wangkangluo1  阅读(428)  评论(0编辑  收藏  举报