stl regex

stl 中regex 非常好用,即使不用boost库也可以很轻易地且高效地摘取你想要的内容,这里提供一个class
解析出 a href 的文件名路径和文件名和文件后缀名
    另外需要解析url的朋友可以看这篇文章:
解析url的代码

代码 show me the code

/*author:qianbodate  :2014-11-06功能  :解析html A href 取出其中的连接,解析出全路径 文件前缀 文件后缀
*/#include <regex>
#include <string>
#include <iostream>
#include <fstream>
#include <functional>
#include <windows.h>
using namespace std;typedef struct dataHtmlFile
{string s1;         //全路径string filePrefix; //文件前缀 string fileSuffix; //带dot的文件后缀 如 .png .jpg
}dataHtmlFile;template <class Type>
class PrintString
{public:PrintString(){}void operator ( ) ( Type& elem ) const{cout<<"the value is "<<elem.s1<<" "<<elem.filePrefix<<" "<<elem.fileSuffix <<endl;//elem *= Factor;}
};class PrintString2
{public:PrintString2(){}void operator ()(string& elem) const{cout<<"now it is "<<elem<<endl;}void print(){}
};class DGIRegex
{private:public:
//检测是否是电话格式static int isPhoneNumber(string strPhone)  {  static string g_strPhone = "0\\d{2}-\\d{8}";std::tr1::cmatch res1;  std::tr1::regex rx(g_strPhone);   if(std::tr1::regex_search(strPhone.c_str(), res1, rx))return 0;return -1;  }  //得到全路径static int get_exe_path(string& strPath){char szPath[260]; if( !GetModuleFileNameA(NULL, szPath, 260 ) ){return -1;}else{(strrchr(szPath,'\\'))[1] = 0;//(_tcsrchr(szPath, '\\'))[1] = 0;strPath = szPath;return 0;}}protected:int UseIter(string& html,vector<dataHtmlFile>& links);int UseSearch(string& html,vector<dataHtmlFile>& links);
public:int RegexSearchLinkFile(string& html,int isFile,vector<dataHtmlFile>& Links);
public:DGIRegex(void);~DGIRegex(void);
};

实现

int DGIRegex::UseIter(string& html,vector<dataHtmlFile>& links)
{tr1::regex rxHtmlB("<A HREF=\"([^>]+)\">",tr1::regex::icase);  //<a[^>]+>tr1::sregex_token_iterator ite(html.begin(), html.end(), rxHtmlB), end;  tr1::regex rx("\"([^\"]+)\"");int i = 0;for ( ; ite != end; ++ite)  {  string strfind = ite->str();//cout << ite->str() << endl;  tr1::cmatch res;if(std::tr1::regex_search(strfind.c_str(),res,rx)){//cout<<res[1]<<endl;//links.push_back(res[1]);i++;}}return i;
}int DGIRegex::UseSearch(string& html,vector<dataHtmlFile>& links)
{tr1::smatch m;tr1::regex rxHtml("<A\\s+HREF\\s*=\\s*\"([^>]+)\">(\\w+)(\\.[^<]+)</A>",tr1::regex::icase);  string source = html;int i = 0;while (tr1::regex_search (source,m,rxHtml)) {//for (auto x:m) std::cout << x << " ";//cout<<m[0];//std::cout << std::endl;//cout<<"this is total "<<m.size()<<endl;dataHtmlFile dhtmlf;dhtmlf.s1 = m[1];dhtmlf.filePrefix = m[2];//文件后缀赋值dhtmlf.fileSuffix = m[3];links.push_back(dhtmlf);++i;source = m.suffix().str();}return i;
}/*输入html 的字符串或者html的名字*/
int DGIRegex::RegexSearchLinkFile(string& html,int isFile,vector<dataHtmlFile>& Links)
{string fileContent = html;if(isFile == 1) //如果是文件,html是文件民称{//fstream eam(html);ifstream in(html.c_str(), ios::in|ios::binary);if(in.is_open()){string temp;while(getline(in,temp)){fileContent += temp;}in.close();return UseSearch(fileContent,Links);}}return UseSearch(fileContent,Links);}

测试程序

int main()
{DGIRegex regex;vector<dataHtmlFile> test;regex.RegexSearchLinkFile(string("c:\\test2.html"),1,test);std::for_each(test.begin(), test.end(),PrintString<dataHtmlFile>());return 0;
}

c++ 使用正则匹配url相关推荐

  1. html正则表达式识别网址,JS正则匹配URL网址的方法(可匹配www,http开头的一切网址)...

    本文实例讲述了JS正则匹配URL网址的方法.分享给大家供大家参考,具体如下: 最强的匹配网址-url的正则表达式:匹配www,http开头的一切网址 直接插入正则表达式: [a-zA-Z0-9][-a ...

  2. 正则匹配html中url,JS正则匹配URL网址的方法(可匹配www,http开头的一切网址)

    本文实例讲述了JS正则匹配URL网址的方法.分享给大家供大家参考,具体如下: 最强的匹配网址-url的正则表达式:匹配www,http开头的一切网址 直接插入正则表达式: [a-zA-Z0-9][-a ...

  3. JS 正则匹配 URL

    //JS 正则匹配 URL var regexp = /((http|https):\/\/([\w\-]+\.)+[\w\-]+(\/[\w\u4e00-\u9fa5\-\.\/?\@\%\!\&a ...

  4. 利用正则匹配url是否合法对于有的url会浪费过长时间使程序卡死,切记!

    改进:改成匹配url是否为以某个结尾的,至于非法的url就让Jsoup.connect(url)把异常抛弃 //启动该正则匹配特别的慢 // public static String regex = ...

  5. php正则匹配是否为url地址,php正则匹配网址-正则php-php正则匹配url地址

    php正则表达式 正则匹配网址是否带http:// https:// if(preg_match("/\x20*https?\:\/\/.*/i","",$m) ...

  6. Python 正则 匹配URL

    (https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|] 参考:正确匹配URL的正则表达式 - 申 ...

  7. js 正则匹配URL,网址,带端口,带query的

    function isURL(url) { const strRegex = '^((https|http|ftp)://)?'//(https或http或ftp):// 可有可无 + '(([\\w ...

  8. 正则匹配url android,Android利用正则表达式如何匹配URL

    在Android项目中遇到一个问题,需求是需要获取浏览器分享出来的内容中的URL. 正常情况下分享出来的URL信息是包含在Bundle的intent.EXTRA_TEXT字段中的,而Tittle信息一 ...

  9. jquery正则匹配URL地址

    JQuery代码: var regexp = /((http|ftp|https|file):\/\/([\w\-]+\.)+[\w\-]+(\/[\w\u4e00-\u9fa5\-\.\/?\@\% ...

最新文章

  1. PHP5.4新特性(转)
  2. 2015.7.11js-10(无缝滚动)
  3. 【机器学习】机器学习从零到掌握之五 -- 教你使用归一化数值准备数据
  4. mysql测试权限_MySQL运行中被改权限测试
  5. access 此程序未正确安装_一款让“微软爸爸杀不死”的数据库软件——生于1992的Access...
  6. javaScript中简单数据类型和复杂数据类型赋值拷贝的理解
  7. carrot2聚类的不同聚类算法 选用方法
  8. DEA_Malmquist指数模型学习笔记
  9. 给Java程序猿们推荐一些值得一看的好书
  10. 好好编程-物流项目20【客户管理-删除客户】
  11. 英语不规则动词变化表
  12. java中的Environment类
  13. 北京科技大学本科毕业论文答辩和论文选题PPT模板
  14. 案例分析 | 茶饮如何积累3500万私域流量实现弯道超车?
  15. cocos 中每个节点的visit与draw函数
  16. service层的作业+mybatis中的重要组件
  17. PDF尺寸怎么调整?两个实用途径
  18. Ubuntu 11.04 更新源(ubuntu yuan)
  19. 免费DSP开发板,你想要吗?
  20. sae php mysql数据库,php连接mysql数据库(新浪云SAE)

热门文章

  1. Visual C# 打造 “浏览器”
  2. c语言电子地图程序,C语言 电子地图信息
  3. tensor backward_Pytorch中的backward函数
  4. 星巴克全面上线美团外卖 并联合美团推出“1971客厅”
  5. 双十一临近 乐视商城宣布全场包邮
  6. 曝华为Mate 50正在测试:有望搭载骁龙898旗舰芯
  7. 顺丰多收一元,这合理吗?
  8. 特斯拉全自动驾驶订阅包来了:199美元包月 老用户99美元
  9. 预装鸿蒙系统!华为MatePad Pro 2或暂定6月2日发布
  10. iPhone 13贴膜渲染图曝光:近几代外观最大升级