在C++编程中,有一点让人挺遗憾的就是C++尚不支持正则表达式,这让很多用户为了编写支持正则表达式程序而不得不放弃C++。然而,Boost.Regex库填补了C++在这方面的空白,它使C++很好的支持各种引擎的正则表达式。

详细文章链接:http://club.topsage.com/thread-2276543-1-1.html

boost regex笔记:http://blog.csdn.net/shiwei0124/article/details/4553665

在C++编程中,有一点让人挺遗憾的就是C++尚不支持正则表达式,这让很多用户为了编写支持正则表达式程序而不得不放弃C++。然而,Boost.Regex库填补了C++在这方面的空白,它使C++很好的支持各种引擎的正则表达式。

结合我的学习,逐步分析Boost.Regex库。

……

Boost.Regex默认使用Perl正则表达式,关于Perl正则表达式的使用,这里就不多说明了,可以参考相关资料。

Boost的正则表达式封装在boost::basic_regex对象中,与std::basic_string一样,boost::basic_regex表示的是一族类,也与std::basic_string一样typedef了几个特例:

typedef basic_regex<char> regex;

typedef basic_regex<wchar>wregex;

Boost.Regex的几个中重要的成员函数如下:

1.       explicit basic_regex(const CharT* p,flag_type regex_constants::normal);

该构造函数接受一个包含正则表达式的字符序列,以及一个表示正则表达式所选的参数信息——例如是否大小写或使用什么引擎的正则表达式。这里注意,如果传递的字符序列是无效的正则表达式,则会抛出异常regex_error。

2.       bool emply()const;

该成员函数检测basic_regex示例是否包含有效的正则表达式。

3.       unsigned mark_count() const;

该成员函数返回该basic_regex示例中的正则表达式包含的有标记的子表达式的个数。

4.       flag_type flags()const;

该函数返回一个位掩码,其中包含basic_regex所设置的选项标志。具体标志选项有:icase—忽略字符大小写和JavaScript—regex使用JavaScript语法

另外,Boost.Regex定义了几个自由函数,实现正则表达式的匹配查询及修改:

1.       判断正则表达式时候匹配整个字符串

 1template <typename CharT,typename Allocator,typename traits>
 2
 3bool regex_match(
 4
 5const CharT*, 
 6
 7match_result<const chart*,Allocator>&m,
 8
 9const basic_regex<CharT,traits>& reg,
10
11match_flag_type flags=match_default);
12

2.       查找字符串中与正则表达式匹配的子序列

template <typename CharT,typename Allocator,typename traits>

bool regex_rearch(

const CharT*, 

match_result<const chart*,Allocator>&m,

const basic_regex<CharT,traits>& reg,

match_flag_type flags=match_default);

3.       查找字符串中所有正则表达式的匹配,并根据参数fmt格式化所匹配的串

template <typename CharT,typename Allocator,typename traits>

basic_string<CharT > regex_replace(

const basic_string<CharT >& s,   

const basic_regex<CharT,traits>& reg,

const basic_string<CharT >& fmt,       

match_flag_type flags=match_default);

使用说明

1.       创建regex对象:

Include<boost/regex.hpp>

regex reg(“(.*)”);

2.       regex_match

该函数用来对一个字符串的完全匹配,在很多校验信息中可以广泛使用,具体使用示例见附后的测试代码

3.       regex_rearch

说到这个函数,必须要说明下boost.match_result。 regex_rearch在执行查找时,通过一个match_result类型的对象来报告匹配的自表达式。

match_result主要封装了一个std::vector<sub_match<<…>> >类型的对象,而sub_match类继承自std::pair,主要记录匹配的结果信息。关于match_result和sub_match的详细了解可以阅读boost设计源码:

使用示例查看附后的测试源码。

4.       regex_replace

该函数根据指定的fmt格式化通过正则表达式匹配的子串。需要注意的是,该函数不会修改原字符串,只是将格式化后的结果返回。具体使用示例见附后测试源码。

5.       regex_iterator

通过多次调用regex_rearch我们可以处理所有满足匹配的字串。但是,Regex库还给我们提供了一个更优雅的方法——即通过regex_iterator。通过字符串和正则表达式构造regex_iterator的时候会构建一个match_result的对象用于保存匹配结果信息,再通过重载++运算符达到遍历所有匹配信息的目的。关于regex_iterator的详细了解可以参考Regex的设计源码:

使用示例查看附后的测试源码。

6.       regex_token_iterator

与regex_iterator相似,Regex还提供了一个列举与正则表达式不匹配的子表达式,就是regex_token_iterator。与stl的设计类似,是通过迭代器适配器实现的。这个特性让我们很容易的分割字符串。关于regex_token_iterator的详细了解请查看Regex的设计源码:

7.测试源码

 1#include<iostream>
 2#include<string>
 3#include<cassert>
 4#include<boost/regex.hpp>
 5#include<vector>
 6#include<iterator>
 7
 8bool validate_identify_card(const std::string &s){
 9    boost::regex reg("//d{17}[A-Z0-9]");
10    return boost::regex_match(s,reg);
11}
12class regex_callback{
13    public:
14    template<typename T>
15    void operator()(const T& what){
16        std::cout<<what[1].str()<<std::endl;
17    }
18};
19
20int main(){
21    {
22        boost::regex reg("//d{3}([a-zA-Z]+).(//d{2}|N/A)//s//1");
23        std::string correct="123Hello N/A Hello";
24        std::string incorrect="123Hello 12 hello";
25        assert(boost::regex_match(correct,reg)==true);
26        assert(boost::regex_match(incorrect,reg)==false);
27    }
28    {
29        std::string s="421124598608976345";
30        assert(validate_identify_card(s)==true);
31    }
32    {
33        boost::regex reg("(new)|(delete)");
34        boost::smatch m;
35        int new_counter=0,delete_counter=0;
36        std::string s="Calls to new must be followed by delete.Calling simply new results in a Leak!";
37        std::string::const_iterator it=s.begin();
38        std::string::const_iterator end=s.end();
39        while(boost::regex_search(it,end,m,reg)){//这里参数必须是const属性
40            if(m[1].matched){
41                std::cout<<"The expression(new) matched"<<std::endl;
42                new_counter++;
43            }
44            if(m[2].matched){
45                std::cout<<"The expression(delete) matched"<<std::endl;
46                delete_counter++;
47            }
48            it=m[0].second;
49        }
50        std::cout<<"new_counter="<<new_counter<<std::endl;
51        std::cout<<"delete_counter="<<delete_counter<<std::endl;
52    }
53    {//使用boost::regex_replace
54        boost::regex reg("(colo)(u)(r)",boost::regex::icase|boost::regex::perl);
55        std::string s="Colour,colour,color,colOurize";
56        s=boost::regex_replace(s,reg,"$1$3");//regex_replace不直接修改s的值,而是返回新值
57        std::cout<<s<<std::endl;
58    }
59    {//使用boost::regex_iterator迭代器输出所有匹配项
60        boost::regex reg("(//d+),?");
61        std::string s="1,2,3,4,5,6,7,85,ad2348(,hj";
62        boost::sregex_iterator it(s.begin(),s.end(),reg);
63        boost::sregex_iterator end;
64        for_each(it,end,regex_callback());
65    }
66    {
67        boost::regex reg("/");
68        std::vector<std::string> vec;
69        std::string s="Split/Vulue/Teather/Neusoft/Write/By/Lanwei";
70        boost::sregex_token_iterator it(s.begin(),s.end(),reg,-1);
71        boost::sregex_token_iterator end;
72        while(it!=end)
73            vec.push_back(*it++);
74        copy(vec.begin(),vec.end(),std::ostream_iterator<std::string>(std::cout,"/n"));
75    }
76    {
77        boost::smatch m;
78        boost::regex reg("(new)|(delete)");
79        std::string s="Calls to new must be followed by delete.Calling simply new results in a Leak!";
80        std::string::const_iterator it=s.begin();
81        std::string::const_iterator end=s.end();
82        while(boost::regex_search(it,end,m,reg)){
83            std::cout<<"size="<<m.size()<<std::endl;
84            std::cout<<"m[-2]="<<m[-2]<<std::endl;
85            std::cout<<"m[-1]="<<m[-1]<<std::endl;
86            std::cout<<"m[0]="<<m[0]<<std::endl;
87            std::cout<<"m[1]="<<m[1]<<std::endl;
88            //std::cout<<"m[2].type="<<typeid(m[2].first).name()<<std::endl;
89            std::cout<<"m[2]="<<m[2]<<std::endl;
90           // std::cout<<"m[2].first="<<m[2].first<<std::endl;
91            //std::cout<<"m[2].second="<<m[2].second<<std::endl;
92            it=m[0].second;
93        }
94    }
95    return 0;
96}
97

学习小结:

关于Boost.Regex库的初步学习暂时告以段落。这是个非常有用的库,打破了用户只有通过POSIX C的API实现正则表达式的局限。然而,这是一个伟大的库博大精深的库,以上的了解只不过的凤毛麟角,其内部还有很多的隐藏秘密需要花大量的时间去挖掘,探索。

所以,如果不是纯粹的要长期使用C++boost的话,就不要在Boost里面探索了!!!

Boost正则表达式相关推荐

  1. Boost:正则表达式的实例

    Boost:正则表达式的实例 实现功能 C++实现代码 实现功能 与宏BOOST_TEST_EQ相关的正则表达式的实例 C++实现代码 #include <boost/regex.hpp> ...

  2. Boost正则表达式库regex常用search和match示例 - 编程语言 - 开发者第2241727个问答

    Boost正则表达式库regex常用search和match示例 - 编程语言 - 开发者第2241727个问答 Boost正则表达式库regex常用search和match示例 发表回复 Boost ...

  3. Boost正则表达式的编译与使用方法集

    下载boost 在boost官网上下载任何版本都可以www.boost.org . 将boost压缩包解压到D盘目录下 (我下载的是boost_1_54_0.zip),目录为D:\boost_1_54 ...

  4. C++新特性之五:标准库中的正则表达式扩充

    总第34篇 本文主要对C++11/14标准库中的正则表达式的扩充进行详细总结说明,以方便大家在学习工作过程中参考. 1.正则表达式 正则表达式是描述一种字符串匹配的模式.一般使用正则表达式主要实现下面 ...

  5. C语言读取大文件的问题 内存映射

    2019独角兽企业重金招聘Python工程师标准>>> [Ref] Windows对文件的读写提供了很丰富的操作手段,如: 1. FILE *fp, fstearm...; (C/C ...

  6. VC操作INI文件 ,INI文件操作总结,如何操作INI文件,INI文件使用方法小结

    INI文件简介 在我们写程序时,总有一些配置信息需要保存下来,以便在下一次启动程序完成初始化,这实际上是一种类持久化.将一些信息写入INI文件(initialization file)中,可完成简单的 ...

  7. x-studio(Lua调试器,粒子编辑器,UI编辑器,代码编辑器,csb恢复工具)

    最新版本:x-studio 10.0.9000.29(2020年4月14日更新) 官网: https://x-studio.net 官方教程: https://docs.x-studio.net x- ...

  8. C++,ini文件操作(包含类)

    什么是ini文件? initialization File,即为初始化文件,是windows的系统配置文件所采用的存储格式,统管windows的各项配置.或者作为项目中的配置文件,为整个项目所共用. ...

  9. MFC操作ini文件方法

    转载:https://blog.csdn.net/rayborn1105/article/details/8192142 在我们的程序设计中经常需要对一些参数进行配置,配置好后还要在下一次启动仍然有效 ...

最新文章

  1. 基础设计模式:单例模式+工厂模式+注册树模式
  2. Windows 2003系统也玩图片收藏屏保
  3. pycharm 添加数据库驱动
  4. 写脚本时遇到的一些问题+自己的一些简单总结[小白版]
  5. WebView三个方法区别(解决乱码问题)
  6. 苹果cms10的php.ini目录列表,[苹果cmsV10]常见问题整理官方版
  7. mysql从库延时好高_部署MySQL延迟从库的几个好处
  8. 四、Linux用户管理
  9. SVM --从“原理”到实现
  10. 数学建模——BP神经网络模型Python代码
  11. 编程珠玑第一章习题解答
  12. Java Web学习(1):Web应用程序与Web服务器
  13. 03 三维地图添加切片图层
  14. 如何计算前缀,网络,子网和主机号?
  15. 条码生成器如何生成GS1-128条码
  16. 高德地图路径轨迹起点标点不变_竞品分析之高德地图与百度地图
  17. 《游戏脚本的设计与开发》-(RPG部分)3.6 队员列表和人物属性
  18. Java经典面试:源码解读及如何保证线程安全
  19. 国内CRM竞品分析【纷享销客 VS 销售易 VS 用友】
  20. arduino 计时器和中断

热门文章

  1. Java使用JDBC连接随意类型数据库(mysql oracle。。)
  2. 华为nova好不好 先看图
  3. 解决【Unable to find the requested .Net Framework Data Provider. It may not be installed.】错误...
  4. 使用组策略中的首选项更改域中计算机注册表
  5. entity framework学习笔记
  6. WPF内存泄露:CollectionViewSource.GetDefaultView导致Cache对象
  7. VC++新建选择卡的解释
  8. 使用taro命令(taro convert)转h5碰到的一些问题
  9. Jeecg-Boot前后端分离,针对敏感数据,加密传递方案
  10. Luogu3732 [HAOI2017] 供给侧改革 【后缀数组】【线段树】【乱搞】