zoukankan      html  css  js  c++  java
  • boost xpressive 例子

    1.效果图:



    我有一个wordpress博客,每次在csdn上写完博客,都需要复制到wordpress中,还需要手动修改<pre>和图片地址,比较麻烦,所以做了这个工具。

    功能:

    1.把CSDN博客的文章中的<pre name="code" class="cpp">标签转换成自定义的标签。比如我的wordpress博客中用的代码加亮插件是SyntaxHighlighter他的代码标签是<pre class="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="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="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

  • 相关阅读:
    5.1、字符串插入
    2.2、部署 Discuz!
    7.1.5、测试数组
    4.2、php 注释
    5.2、操作符
    2.3、初始化 Discuz!
    5.3、控制结构
    gradle 又一项目构建工具
    1.1、概述
    7.1.8、通过追加数组的方式创建数组
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3215063.html
Copyright © 2011-2022 走看看