boost xpressive 例子
来源:互联网 发布:淘宝直通车怎么卡位 编辑:程序博客网 时间:2024/05/02 00:54
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 例子
- boost xpressive
- boost.xpressive
- boost xpressive
- boost xpressive简明用法
- Boost xpressive 正则表达式
- boost xpressive的用法1
- boost.xpressive,Formatter的用法
- boost的xpressive编译错误如何解决?
- boost的xpressive的正则表达式
- Boost 使用xpressive正则表达式错误
- boost xpressive库和string_algo库
- xpressive是boost的正则表达式库
- boost xpressive库正则表达式使用笔记
- Performance: std::regex v.s. boost::regex v.s. boost::xpressive v.s. boost::spirit::qi
- boost正则表达式库regex库和xpressive库关于零宽断言的问题
- boost xpressive库,介于正则和bnf库之间的混血。显示c/c++代码注释部分。
- 关于boost的regex库和xpressive库匹配所有标点(含中文标点)的方法
- 真正的跨平台运行
- ubuntu清理系统垃圾与备份
- sqlite3中的数据类型
- ApiDemos之ActionBarTab
- HDU-1233-还是畅通工程
- boost xpressive 例子
- poj 3613 Cow Relays
- 四元数一般定义如
- centos6.3搭建java web环境
- 常见期刊
- 免费api
- DMA描述符及映射
- Delphi基础:回调函数及其使用
- 解决IE(IE6/IE7/IE8)不兼容HTML5标签的问题[转自大神]