加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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( &reg,pattern,cflags);
	if (z != 0) {
		regerror(z,&reg,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(&reg,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(&reg);
	return 0;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读