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 例子的更多相关文章

  1. 学习懈怠的时候,可以运行Qt自带的Demo,或者Delphi控件自带的Demo,或者Cantu书带的源码,运行一下Boost的例子(搞C++不学习Boost/Poco/Folly绝对是一大损失,有需要使用库要第一时间想到)(在六大的痛苦经历说明,我的理论性确实不强,更适合做实践)

    这样学还不用动脑子,而且熟悉控件也需要时间,而且慢慢就找到感觉了,就可以精神抖擞的恢复斗志干活了.或者Cantu书带的源码. 并且可以使用Mac SSD运行Qt的Demo,这样运行速度快一点. 此外, ...

  2. boost - 正则表达式xpressive

     正则表达式是一套处理文本强有力的工具: 它使用一套复杂的语法规则,可以解决文本处理领域的绝大多数问题; 而这些问题通常是字符串算法很难甚至无法解决的. C++98标准中没有内置的正则表达式支持,使得 ...

  3. (九)boost库之文件处理filesystem

    (九)boost库之文件处理filesystem   filesystem库是一个可移植的文件系统操作库,它在底层做了大量的工作,使用POSIX标准表示文件系统的路径,使C++具有了类似脚本语言的功能 ...

  4. Boost filessystem...

    CMakeList.txt: cmake_minimum_required(VERSION 3.8) project(Demo) ) set(SOURCE_FILES main.cpp) //需要添加 ...

  5. windows下编译和安装boost库

    boost是一个功能强大.构造精巧.跨平台.开源并且完全免费的C++程序库. 获取方式 boost提供源码形式的安装包,可以从boost官方网站下载,目前最新版本是1.59.0. 本机上正好有boos ...

  6. boost shared_ptr weak_ptr

    文档: http://www.boost.org/doc/libs/1_57_0/libs/smart_ptr/shared_ptr.htm shared_ptr构造有个原型 template< ...

  7. boost之正则表达式

    正则表示主要用于查找一系列符合规则的对象,而我们之前的查找是对某一特定的字符串进行查找. #include <iostream> #include <vector> #incl ...

  8. boost regex expression

    Boost.Regex provides three different functions to search for regular expressions 1. regex_match #inc ...

  9. c++ shared_ptr 使用注意事项. 1

    条款1:不要把一个原生指针给多个shared_ptr管理 int* ptr = new int; shared_ptr<int> p1(ptr); shared_ptr<int> ...

随机推荐

  1. Zepto

    移动开发流量省起来之Zepto 事情是这样的:最近开发的一个手机网站客户反应访问起来特别慢,刷了半天才能看到页面,然后问我们是不是网站出问题了.于是我赶紧找了各种手机测试一下,没有问题,首先排除程序错 ...

  2. jQuery选中该复选框来实现/全部取消/未选定/获得的选定值

    <!DOCTYPE html> <html> <head> <script type="text/javascript" src=&quo ...

  3. node.js + mongodb

    node.js + mongodb 这次内容是结合bootstrap把登陆注册做好,还有就是express的中间件等问题. 看这篇博客之前建议先看我上篇写的那篇博客http://www.cnblogs ...

  4. MySQL replace into 说明(insert into 增强版)

    MySQL replace into 说明(insert into 增强版) 在插入数据到一个表时,通常是这种情况:1. 先推断数据是否存在: 2. 假设不存在,则插入:3.假设存在,则更新. 在 S ...

  5. Spring IOC之Classpath扫描和管理的组件

    在前面的大部分例子我们使用XML去指明配置数据去定义在Spring容器中的每一个BeanDefinition.上一节我们展示了如何在 代码层注解的方式来提供大量的配置信息.即使在这些例子中,但是,基础 ...

  6. openwrt_git_pull命令提示merger冲突时如何解决?

    直接贴代码 tf@ubuntu:~/projects/openwrt1407$ git pull Updating 331ecb0..d12dc6e error: Your local changes ...

  7. C#中使用消息队列RabbitMQ

    在C#中使用消息队列RabbitMQ 2014-10-27 14:41 by qy1141, 745 阅读, 2 评论, 收藏, 编辑 1.什么是RabbitMQ.详见 http://www.rabb ...

  8. vs 文件头自动添加注释

    原文:vs 文件头自动添加注释 vs2010 C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates ...

  9. Mysql 使用 select into outfile

    Mysql支持将查询结果到处 默认语法 select .. from table  into outfile "filepath\filename.txt"; 如果在执行的过程中遇 ...

  10. Libgdx Box2D现实---这缓释微丸(两:Box2D介绍)

     Box2D官方网站 : http://box2d.org/ Box2D v2.1.0用户手冊翻译 : http://blog.csdn.net/complex_ok/article/catego ...