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. nagios监控+pnp4出图
  2. 最近面试了一位5年的Java,一问三不知!还反怼我...
  3. 沈向洋官宣离职微软!他是微软级别最高的中国人、微软AI领导者,21年前参与创办MSRA...
  4. POSTMAN 数据关联
  5. html 下拉框字体,怎么把select下拉菜单里的文字设置成左右滚动效果
  6. JUnit学习摘要+入门实例 (junit4)
  7. 给ubuntu换个图标主题(icon theme)
  8. 聚焦2020云栖大会 边缘计算专场畅谈技术应用创新
  9. 人人都能成为安全防范的高手 ——《黑客新型攻击防范:深入剖析犯罪软件》...
  10. 精品|从零开始-基于FPGA 的软核处理器设计实现
  11. 找不到或无法加载主类什么意思(找不到或无法加载主类)
  12. LiveGBS如何配置安防摄像头云端录像存储回放
  13. 黑客突破防火墙常用的几种技术(转)
  14. Java5、8、9章复习
  15. 2015.3.30第一次博客测试
  16. fhq treap入门
  17. 关于STM32与OpenMv通讯踩过的那些坑(1)
  18. 为什么很多程序员没有升级到架构师?
  19. Linux 终极装机指南
  20. 网络字节序和主机字节序详解

热门文章

  1. 毕业之后,这些年薪 50w+ 的 90 后程序员都经历了什么?纯水贴
  2. zynq中纯Programmable Loigc编程
  3. apache是怎么运行php的_PHP与WEB服务器是如何交互的
  4. 抖音测试快递服务“音尊达” 已接入中通、圆通等,可送货上门
  5. 罗永浩吐槽苹果功能更改 @库克:不要再胡来 做祸害用户体验的事
  6. 三星公布三款新型车用芯片 向大众供应
  7. 造车梦又要“窒息”了?贾跃亭被美国认定骗局,收到退市警告!FF回应了......
  8. 阿里云、百度云被约谈 督促落实防范治理电信网络诈骗
  9. 8599元起!三星Galaxy Z Fold3/Flip3 5G折叠手机国行即将发货
  10. 小红书8月2日正式推行“号店一体”机制 月销万元以下商家免收佣金