boost xpressive 例子

网友投稿 687 2022-11-09

boost xpressive 例子

boost xpressive 例子

1.效果图:

我有一个wordpress博客,,都需要复制到wordpress中,还需要手动修改

和图片地址,比较麻烦,所以做了这个工具。

2. boost xpressive

本文假定读者已经-了boost,并且编译了boost。

首先看第一个需求,把

转换成

这个项目难的是正则表达式的写法,倒不是xpressive中sregex,regex_replace,sregex_iterator的应用。

首先要通过正则表达式得到cpp。

我写的正则表达式是  (?:

)

解释下这个正则表达式的作用。

首先是3对小括号,会匹配四个组:

再看下?的作用,(后面跟?:是表示不将这个匹配结果加入到组中.

这样就只剩下两个组了。

和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"

转换成
完整代码

看不懂的还是看下《Boost程序库完全开发指南》206页关于sregex_iterator的说明。

string replaceCodeString(string& inString, const string& customedPreString){ sregex reg = sregex::compile("(?:

)");  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("
" ,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-" to "waitingfy.com/wp-content/uploads/2013/07" sregex urlRegex = sregex::compile("img.blog.csdn-"); 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写的。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:HeapSort(堆排序 C++)
下一篇:cocos2d 3.0 屏幕适配
相关文章

 发表评论

暂时没有评论,来抢沙发吧~