vs 2010 正则表达式校验
发布时间:2020-12-14 04:17:57 所属栏目:百科 来源:网络整理
导读:#include sys/types.h#include "regex.h"#include stdio.h#include string.h/* 取子串的函数 */static char* substr(const char*str,unsigned start,unsigned end){unsigned n = end - start;static char stbuf[256];strncpy(stbuf,str + start,n);stbuf[n]
#include <sys/types.h> #include "regex.h" #include <stdio.h> #include <string.h> /* 取子串的函数 */ static char* substr(const char*str,unsigned start,unsigned end) { unsigned n = end - start; static char stbuf[256]; strncpy(stbuf,str + start,n); stbuf[n] = 0; return stbuf; } /* 主程序 */ int main(int argc,char** argv) { char * pattern = ""; int x,z,lno = 0,cflags = REG_EXTENDED | REG_NOSUB ; char ebuf[128]; char lbuf[256] = ""; regex_t reg; regmatch_t pm[10]; const size_t nmatch = 10; /* 编译正则表达式*/ //pattern = argv[1]; z = regcomp( ®,pattern,cflags); if (z != 0) { regerror(z,®,ebuf,sizeof(ebuf)); fprintf(stderr,"%s: pattern '%s' n",pattern); return 1; } /* 逐行处理输入的数据 */ //while (fgets(lbuf,sizeof(lbuf),stdin)) { // ++lno; if ((z = strlen(lbuf)) > 0 && lbuf[z - 1] == 'n') lbuf[z - 1] = 0; /* 对每一行应用正则表达式进行匹配 */ z = regexec(®,lbuf,nmatch,pm,0); if (z == REG_NOMATCH) return 0; else if (z != 0) { regerror(z,sizeof(ebuf)); fprintf(stderr,"%s: regcom('%s')n",lbuf); return 2; } /* 输出处理结果 */ for (x = 0; x < nmatch && pm[x].rm_so != -1; ++x) { if (!x) printf("%04d: %sn",lno,lbuf); printf("$%d='%s'n",x,substr(lbuf,pm[x].rm_so,pm[x].rm_eo)); } //} /* 释放正则表达式 */ regfree(®); return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- c# – 使用List.Any()查找字符串是否包含项目以及查找匹配项
- objective-c – 当前日期和时间 – NSDate
- ruby-on-rails – 如何附加回WEBrick服务器
- 如何在C#LINQ中构建高效的查询来过滤数据
- 关于Msp430Flash型单片机内…
- LPC1768 Flash 存储器加速模块
- c# – 带有数字和特殊字符但没有字母的正则表达式
- 为什么我不能在PostgreSQL中设置这个唯一约束?
- ios – MSMessagesAppViewController didStartSending重写方
- c# – 如何在CookieContainer中获取Cookie信息? (所有他们