linux下用C写正则匹配IP和邮箱
来源:互联网 发布:数控铣床编程实例 编辑:程序博客网 时间:2024/05/23 15:46
一时想娱乐一下,写了一段小程序,使用linux的正则库匹配ip和email地址
#include <sys/types.h>#include <regex.h>#include <iostream>#include <string>#include <map>#include <algorithm>#include <cctype>using namespace std;string strtolower(string str);int main(int argc, const char *argv[]){ if (argc < 3) { cout << "usage: regex <type> string" << endl; return 0; } string type, str; int result, z, reti; regmatch_t pm[10]; const size_t nmatch = 10; type = string((const char*)argv[1]); str = string(argv[2]); map<string, string> regs; map<string, int> types; regs["ip"] = "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}"; regs["email"] = "[a-zA-Z0-9_.-]+@[a-zA-Z0-9_.-]+\\.[a-zA-Z0-9_.-]+"; types["ip"] = 1; types["email"] = 2; regex_t preg; type = strtolower(type); reti = regcomp(&preg, regs[type].c_str(), REG_EXTENDED|REG_NOSUB); if (reti) { char errbuf[1024]; regerror(reti, &preg, errbuf, 1024); cout << "Could not compile regex with message " << errbuf;exit(1); } result = regexec(&preg, str.c_str(), 0, NULL, 0); switch (types[type]) { case 1 : if (result == REG_NOMATCH) cout << "regcomp ip no match" << " reg express " + regs[type] << endl; else if (!result) { cout << "match" << endl; } break; } regfree(&preg); return 0;}stringstrtolower(string str){ string tmp; for(size_t i=0; i<str.length(); ++i) tmp.push_back(tolower(str[i])); return tmp;}
- linux下用C写正则匹配IP和邮箱
- Java正则表达式IP地址和邮箱匹配
- 手机号和邮箱正则匹配
- 手机号码 邮箱 IP地址的正则匹配
- 正则 去除html文本和匹配邮箱
- JS正则座机电话和邮箱匹配
- js 手机号和邮箱正则匹配
- Java邮箱正则匹配
- 匹配邮箱正则表达式
- 正则表达式匹配邮箱
- 正则表达式匹配邮箱
- linux下匹配ip地址
- Python-同时匹配邮箱和电话号码的正则表达式
- [Regex ]正则表达式匹配ip和域名
- PHP正则 匹配中文 邮箱
- 正则表达式匹配邮箱账号
- 正则表达式之匹配邮箱
- 正则表达式邮箱匹配详解
- C语言基本类型转换
- MFC 实现类似QQ自动靠边功能
- 二维数组中查找整数
- C++生成默认构造函数的条件
- SPOJ1440 Use of Function Arctan
- linux下用C写正则匹配IP和邮箱
- 吐槽 uestc oj
- jQuery plugin: Validation 使用说明
- CentOS 5.3上安装Apache+php+Mysql+phpMyAdmin
- MongoDB010 索引
- ubuntu12.04 32bits install python3.3
- include 与 require的区别
- 教你如何迅速秒杀掉:99%的海量数据处理面试题
- HTTP协议详解