本文实例讲述了C语言正则表达式操作。分享给大家供大家参考,具体如下: #include stdio.h#include sys/types.h#include regex.hint main(int argc,char**argv){ int status; int i; int cflags = REG_EXTENDED; regmatch_t
本文实例讲述了C语言正则表达式操作。分享给大家供大家参考,具体如下:
#include <stdio.h>
#include <sys/types.h>
#include <regex.h>
int main(int argc,char**argv)
{
int status;
int i;
int cflags = REG_EXTENDED;
regmatch_t pmatch[1];
const size_t nmatch =1 ;
regex_t reg;
const char * pattern="^[A-Z]{2}\\w+@\\w{6}_\\w+.\\w+$";
//const char * pattern="^[A-Z]{2}\\w+$";
//const char * pattern="^\\w$";
regcomp(®,pattern,cflags);
status=regexec(®,argv[1],nmatch,pmatch,0);
printf("%s",argv[1]);
if(status == REG_NOMATCH)
printf("no Match\n");
else if(status ==0)
{
printf("match\n");
}
}
可匹配类似admin@tools_jb51.net格式的字符串
PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:
JavaScript正则表达式在线测试工具:
http://tools.jb51.net/regex/javascript
正则表达式在线生成工具:
http://tools.jb51.net/regex/create_reg
希望本文所述对大家C语言程序设计有所帮助。
