boost xpressive 例子
1.效果图:
我有一个wordpress博客,每次在csdn上写完博客,都需要复制到wordpress中,还需要手动修改<pre>和图片地址,比较麻烦,所以做了这个工具。
功能:
1.把CSDN博客的文章中的<pre name="code" class="cpp">标签转换成自定义的标签。比如我的wordpress博客中用的代码加亮插件是SyntaxHighlighter他的代码标签是<pre class="brush:cpp;" >
2.把CSDN博客的文章中的图片标签转换成Wordpress博客中的图片地址。比如会把"http://img.blog.csdn.net/20130621230257406"转换成"http://www.waitingfy/wp-content/uploads/2013/07/20130621230257406.jpg"
2. boost xpressive
本文假定读者已经下载了boost,并且编译了boost。
首先看第一个需求,把<pre name="code" class="cpp">转换成<pre class="brush:cpp;" >。
这个项目难的是正则表达式的写法,倒不是xpressive中sregex,regex_replace,sregex_iterator的应用。
首先要通过正则表达式得到cpp。
我写的正则表达式是 (?:<pre name=\"code\" class=\")(.*?)(?:\">)
解释下这个正则表达式的作用。
首先是3对小括号,会匹配四个组: <pre name="code" class="cpp">
<pre name="code" class="
cpp
">。
再看下?的作用,(后面跟?:是表示不将这个匹配结果加入到组中.
这样就只剩下两个组了。<pre name="code" class="cpp">和cpp。用(*pos)[1]就能得到我们要的cpp了。
.*?这个也很关键,允许我复制一段文字解释下。
贪婪匹配
在满足匹配时,匹配尽可能长的字符串,默认情况下,采用贪婪匹配
string pattern1 = @"a.*c"; // greedy match
Regex regex = new Regex(pattern1);
regex.Match("abcabc"); // return "abcabc"
非贪婪匹配
在满足匹配时,匹配尽可能短的字符串,使用?来表示非贪婪匹配
string pattern1 = @"a.*?c"; // non-greedy match
Regex regex = new Regex(pattern1);
regex.Match("abcabc"); // return "abc"
把<pre name="code" class="cpp">转换成<pre class="brush:cpp;" >完整代码:
看不懂的还是看下《Boost程序库完全开发指南》206页关于sregex_iterator的说明。
string replaceCodeString(string& inString, const string& customedPreString){
sregex reg = sregex::compile("(?:<pre name=\"code\" class=\")(.*?)(?:\">)");
sregex_iterator pos(inString.begin(),inString.end(),reg);
sregex_iterator end;
string copyStr = inString;
sregex replaceCodeRegex;
while(pos != end){
string language = (*pos)[1];
replaceCodeRegex = sregex::compile("<pre name=\"code\" class=\"" + language + "\">" ,icase);
char customedPre[100];
sprintf_s(customedPre,customedPreString.c_str(),language.c_str());
copyStr = regex_replace(copyStr,replaceCodeRegex,customedPre);
++pos;
}
return copyStr;
}
转换图片也是类似的代码:
string replaceImgString(string& inString, const string& domain){
//first add ".jpg"
sregex addJpgRegex = sregex::compile("\" alt=\"\" />");
inString = regex_replace(inString, addJpgRegex, ".jpg\" alt=\"\" />");
//second replace "img.blog.csdn.net" to "www.waitingfy.com/wp-content/uploads/2013/07"
sregex urlRegex = sregex::compile("img.blog.csdn.net");
time_t now; time(&now);
char s[100];
struct tm *ttime = localtime(&now);
sprintf_s(s,"%s/wp-content/uploads/%02d/%02d",domain.c_str(),ttime->tm_year + 1900,ttime->tm_mon + 1);
inString = regex_replace(inString,urlRegex,s);
//third replace "?watermark......" to ".jpg"
string copyStr = inString;
sregex findWatermarkRegex = sregex::compile("(\\?watermark.*?)(?:\" alt=\"\" />)");
sregex_iterator pos(inString.begin(),inString.end(),findWatermarkRegex);
sregex_iterator end;
sregex replaceToJpgRegex;
while(pos != end){
replaceToJpgRegex = sregex::compile("\\" + (*pos)[1],icase);
copyStr = regex_replace(copyStr,replaceToJpgRegex,".jpg");
++pos;
}
return copyStr;
}
你们还是下载整个项目看下吧。是用vs2005写的。
项目下载地址:http://www.waitingfy.com/?attachment_id=604
文章源地址:http://www.waitingfy.com/?p=592
boost xpressive 例子的更多相关文章
- 学习懈怠的时候,可以运行Qt自带的Demo,或者Delphi控件自带的Demo,或者Cantu书带的源码,运行一下Boost的例子(搞C++不学习Boost/Poco/Folly绝对是一大损失,有需要使用库要第一时间想到)(在六大的痛苦经历说明,我的理论性确实不强,更适合做实践)
这样学还不用动脑子,而且熟悉控件也需要时间,而且慢慢就找到感觉了,就可以精神抖擞的恢复斗志干活了.或者Cantu书带的源码. 并且可以使用Mac SSD运行Qt的Demo,这样运行速度快一点. 此外, ...
- boost - 正则表达式xpressive
正则表达式是一套处理文本强有力的工具: 它使用一套复杂的语法规则,可以解决文本处理领域的绝大多数问题; 而这些问题通常是字符串算法很难甚至无法解决的. C++98标准中没有内置的正则表达式支持,使得 ...
- (九)boost库之文件处理filesystem
(九)boost库之文件处理filesystem filesystem库是一个可移植的文件系统操作库,它在底层做了大量的工作,使用POSIX标准表示文件系统的路径,使C++具有了类似脚本语言的功能 ...
- Boost filessystem...
CMakeList.txt: cmake_minimum_required(VERSION 3.8) project(Demo) ) set(SOURCE_FILES main.cpp) //需要添加 ...
- windows下编译和安装boost库
boost是一个功能强大.构造精巧.跨平台.开源并且完全免费的C++程序库. 获取方式 boost提供源码形式的安装包,可以从boost官方网站下载,目前最新版本是1.59.0. 本机上正好有boos ...
- boost shared_ptr weak_ptr
文档: http://www.boost.org/doc/libs/1_57_0/libs/smart_ptr/shared_ptr.htm shared_ptr构造有个原型 template< ...
- boost之正则表达式
正则表示主要用于查找一系列符合规则的对象,而我们之前的查找是对某一特定的字符串进行查找. #include <iostream> #include <vector> #incl ...
- boost regex expression
Boost.Regex provides three different functions to search for regular expressions 1. regex_match #inc ...
- c++ shared_ptr 使用注意事项. 1
条款1:不要把一个原生指针给多个shared_ptr管理 int* ptr = new int; shared_ptr<int> p1(ptr); shared_ptr<int> ...
随机推荐
- typecheck()简析
#define typecheck(type,x) \ ({ type __dummy; \ typeof(x) __dummy2; \ (void)(&__dummy == &__d ...
- jQuery整理您的笔记----jQuery开始
Jquery它是一种高速.简明的JavaScript相框,jQuery设计目标:Write Less,Do More(写更少的代码,做很多其他的事情). 一.Jquery框架优势: 1.轻量级 jQu ...
- inux平台的C与C++
课堂里学不到的C与C++那些事(一) 首先,声明一下这是一个系列的文章.至于整个系列有多少篇,笔者也不知道,不知道有多少篇,也不知道多久会更新一篇.反正只有一个原则,写出来的文 章能见得人才会公布出来 ...
- C语言库函数大全及应用实例十二
原文:C语言库函数大全及应用实例十二 [编程资料]C语言库函数大全及应用实例十二 函数名: setrgbpalette ...
- Java-DES算法加密解密工具类
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import ...
- WebApiContrib
https://github.com/WebApiContrib ASP.NET Web API and Protocol Buffers Protocol Buffers are a super e ...
- Android 通过网络打开自己的APP(scheme)
通过使用手机的浏览器(内部.第三方能够)访问网页,点击一个链接,开始实施自己的应用程序,和传输数据. 第一Mainifest面对文件启动Activity添加过滤器. <activity andr ...
- 关于PHP的工作流引擎
关于PHP的工作流引擎,除了三大主流开源:PorcessMaker(排名第一,因其有拖放式图形定义界面),RadiCore(基于PETRI NET)和CuteFlow以外,另外还有一个不为人知的,但却 ...
- Metadata是.NET平台的核心灵魂--(转载)
(转载)Metadata是.NET平台的核心灵魂 July 7th, 2010 jzli Leave a comment Go to comments 网友来信:李老师,您好!我参加过你去年到我们公司 ...
- WCF总结笔记
------------------------windowform承载服务步骤: (1)定义契约: using System; using System.Collections.Generic; u ...