c/c++语言开发共享[C/C++11]_[初级]_[使用正则表达式库regex]

场景 正则表达式在处理非常量字符串查找,替换时能很省事,如果稍微复杂点的字符串匹配, 没有正则表达式还真做不出来. c++11 为我们提供了正则表达式库. 使用起来比boost的正则库方便. 搞ja


场景

正则表达式在处理非常量字符串查找,替换时能很省事,如果稍微复杂点的字符串匹配, 没有正则表达式还真做不出来. c++11 为我们提供了正则表达式库. 使用起来比boost的正则库方便. 搞java 的一定觉得很搞笑,这都是java的标配功能, 怎么c++11才支持这个库,vs2010 以才支持.建议在处理字符串搜索替换时,直接用正则吧,代码量少,快速.

说明

正则表达式的语法我不多说了,vs2010 如果模式字符串写错,运行也会崩溃.一般调用正则方法时报错基本就是模式字符串写错了. 正则的语法真的很多,向前向后,分组…看参考吧,一般不需要全部学会就够用了.

例子

  <code class="language-html hljs ">  // test_reg.cpp : 定义控制台应用程序的入口点。  //    #include "stdafx.h"  #include <regex>  #include <string>  #include <assert.h>  #include <iostream>    static std::string khtmlsnippet = "<p><img data-cke-saved-src="" src=""d:\doc\个人软件\需求文档\安卓助手\android\images\main\main.png"" width=""30%"" height=""30%""></p>"  "<ol><li>"  "</li><li>ddd中文歌</li><li>"  "</li><li>xxx</li><li>"  "</li></ol>"  "<p><img data-cke-saved-src="" src=""d:\doc\个人软件\需求文档\安卓助手\android\images\main\main-about.png"" width=""30%"" height=""30%""></p>"  "<ol><li>"  "</li><li>xxxxx</li><li>"  "</li></ol>"  "<p><img data-cke-saved-src="" src=""d:\doc\个人软件\需求文档\安卓助手\android\images\main\main-setting.png"" width=""30%"" height=""30%""></p>";    void testreplace()  {      std::cout << "testreplace ====" << std::endl;        // 把所有 img src 的绝对路径替换为 images 开始的相对路径.      // 使用分组即可.      std::regex img_regex("(<img>]*src=["']{1})([^"']*)\\(images\\[^"']*["']{1}[^>]*>)");      std::smatch color_match;        std::string rep = "$1$3";      std::string tmp = std::regex_replace(khtmlsnippet,img_regex,rep);      std::cout << tmp << std::endl;    }    void testsearch()  {      std::cout << "testsearch ====" << std::endl;        // 查找所有的img完整标签      std::regex img_regex("<img>]+>");        // 使用 std::regex_search 查询第一个匹配的字符串.      std::smatch color_match;      std::cout << "regex_search ====" << std::endl;      if(std::regex_search(khtmlsnippet, color_match, img_regex))      {          std::cout << color_match[0] << 'n';      }        // 使用类 std::regex_iterator 来进行多次搜索.      std::cout << "sregex_iterator ====" << std::endl;      auto words_begin =          std::sregex_iterator(khtmlsnippet.begin(), khtmlsnippet.end(), img_regex);      auto words_end = std::sregex_iterator();      for (std::sregex_iterator i = words_begin; i != words_end; ++i)      {          std::smatch match = *i;                                                           std::string match_str = match.str();          std::cout << match_str << 'n';      }  }    int _tmain(int argc, _tchar* argv[])  {      testsearch();      testreplace();      return 0;  }  </iostream></assert.h></string></regex></code>

输出:

  testsearch ====  regex_search ====  <img src="d:doc&#x4e2a;&#x4eba;&#x8f6f;&#x4ef6;&#x9700;&#x6c42;&#x6587;&#x686  3;&#x5b89;&#x5353;&#x52a9;&#x624b;androidimagesmainmain.png" width="30%" he  ight="30%">  sregex_iterator ====  <img src="d:doc&#x4e2a;&#x4eba;&#x8f6f;&#x4ef6;&#x9700;&#x6c42;&#x6587;&#x686  3;&#x5b89;&#x5353;&#x52a9;&#x624b;androidimagesmainmain.png" width="30%" he  ight="30%">  <img src="d:doc&#x4e2a;&#x4eba;&#x8f6f;&#x4ef6;&#x9700;&#x6c42;&#x6587;&#x686  3;&#x5b89;&#x5353;&#x52a9;&#x624b;androidimagesmainmain-about.png" width="3  0%" height="30%">  <img src="d:doc&#x4e2a;&#x4eba;&#x8f6f;&#x4ef6;&#x9700;&#x6c42;&#x6587;&#x686  3;&#x5b89;&#x5353;&#x52a9;&#x624b;androidimagesmainmain-setting.png" width=  "30%" height="30%">  testreplace ====  <p><img src="imagesmainmain.png" width="30%" height="30%"></p><ol><li>ddd中文  歌</li><li>xxx</li></ol><p><img src="imagesmainmain-about.png" width="30%" hei  ght="30%"></p><ol><li>xxxxx</li></ol><p><img src="imagesmainmain-setting.png"  width="30%" height="30%"></p>

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/606871.html

(0)
上一篇 2021年5月14日
下一篇 2021年5月14日

精彩推荐