c语言中的正则
2016-02-20 00:25
330 查看
#include <stdio.h> #include <string.h> #include <regex.h> #define SUBSLEN 10 #define BUFLEN 1024 //储存找到的字符串所需空间 int main() { char* string="asd@asd.com,qwe@qwe.cn,zxc@zxc.org"; char* pattern="[a-zA-Z0-9]+@[a-zA-Z0-9]+\\.[a-zA-Z0-9]{1,3}"; regex_t regex; //编译后的正则储存在这里 regmatch_t subs[SUBSLEN]; //子表达式储存在这里 regcomp(®ex, pattern, REG_EXTENDED); int offset=0; while(regexec(®ex,string+offset,SUBSLEN,subs,0)==0) { static char matched[BUFLEN]; //匹配的字符串 int len=subs[0].rm_eo-subs[0].rm_so; memcpy(matched,string+offset+subs[0].rm_so,len); matched[len]='\0'; printf("match: %s\n", matched); offset+=subs[0].rm_eo+1; } regfree(®ex); return 0; }
相关文章推荐
- 【C++学习与应用总结】12: Unscoped Enum
- C语言杂谈:指针与数组 (上)
- c++STl的全排列
- C++之 浅拷贝和深拷贝
- neuq oj 1043: 谭浩强C语言(第三版)习题5.7 C语言
- 关于C语言include尖括号和双引号的对话
- C语言:【动态顺序表】动态顺序表的在指定位置插入元素Insert,以及指定元素Find
- 单件模式
- 74. Search a 2D Matrix
- 【hdu 2896】病毒侵袭 题解&代码(C++)
- C++之 构造函数调用规则
- 【源代码】将一个整数的每位数分解并按逆序放入一个数组中(用递归算法)(C语言实现)
- Basic C++ Container classes summary(Array, Vector, List)
- c语言入门之项目2.0——成绩等级
- C语言:【动态顺序表】动态顺序表的初始化、打印、尾插PushBack、尾删PopBack
- C++基础篇之 复制构造函数调用时机
- c/c++题目汇总一
- C语言之getopt使用
- 【hdu 2222】Keywords Search 中文题意&题解&代码(C++)
- 「Cplusplus」在命令提示符下编译C/C++源文件和运行可执行文件