Boost学习之正则表达式--regex

本文详细介绍了Boost库中的正则表达式组件,包括基本用法、match_results、sub_match以及reg_match、reg_search和reg_replace等核心算法。通过实例展示了如何在C++中进行字符串匹配、查找和替换操作,还提到了使用regex_iterator和regex_token_iterator进行更高级的字符串处理。
摘要由CSDN通过智能技术生成

来源:http://blog.chinaunix.net/uid-21222282-id-1829265.html

来源:http://www.cnblogs.com/undermoon/archive/2009/11/30/1613508.html

来源:http://blog.csdn.net/guyulongcs/article/details/7838753

来源:http://blog.csdn.net/leonardwang/article/details/6035171

boost::regex类为C++提供了完整的正则表达式支持,并且已被接收为C++0x标准库。它同时也在Boost库中扮演着极重要的角色,不少Boost子库都需要它的支持,有不少人甚至就是为了它才下载使用Boost的。

boost::regex的默认正则表达式语法是perl语法
        boost::regex支持perl regular表达式、POSIX-Extended regular表达式和POSIX-Basic Regular表达式,但默认的表达式语法是perl语法,如果要使用其余两种语法需要在构造表达式的时候明确指定。

//例如,下面两种方法效果相同
// e1 is a case sensitive Perl regular expression:
// since Perl is the default option there''s no need to explicitly specify the syntax used here:
 boost::regex e1(my_expression);
// e2 a case insensitive Perl regular expression:
boost::regex e2(my_expression, boost::regex::perl|boost::regex::icase);
boost::regex对unicode编码的支持
        boost::regex使用ICU来实现对unicode及unicode变种的支持,这需要在编译boost的时候指出是否使用ICU以及ICU所在 的目录。否则编译出来的boost::regex不支持unicode编码。其中boost::wregex支持unicode编码的搜索,如果要搜索 UTF-8、UTF-16、UFT-32编码的字符串,则要用boost::u32regex。注意boost::wregex只能支持unicode编 码,不能支持uft编码。
搜索时如何忽略大小写
        如果要在搜索时忽略大小写(即大小写不敏感),则要用到表达式选项boost::regex::icase,例如: boost::regex e2(my_expression, boost::regex::perl|boost::regex::icase); Boost Regex Libray类和接口介绍
(1)basic_regex
basic_regex是一个模板类,它封装了正则表达式的解析和编译,它是Boost.Regex中用来表示正则表达式的对象类型。Boost.Regex定义了两种标准类型,一种是基于单字节字符的regex,另一种是基于宽字符的wregex
关于basic_regex提供的接口,和STL中basic_string所提供的十分类似,具体可以参考: http://www.boost.org/doc/libs/1_37_0/libs/regex/doc/html/boost_regex/ref/basic_regex.html

(2)match_results
match_results是用来表示所有匹配指定正则表达式的字符串的集合的对象类型。Boost.Regex提供了四种标准类型的定义:C单字节字符类型的cmatch, C宽字符类型的wcmatch, C++单字节字符类型smatch, C++宽字符类型wsmatch。match_results所提供的接口参见:http://www.boost.org/doc/libs/1_37_0/libs/regex/doc/html/boost_regex/ref/match_results.html

(3)sub_match
sub_match是用来表示匹配指定正则表达式的字符串的对象类型。match_results就是由sub_match组成的集合类型。
关于sub_match类型,有下面三点需要注意的:
a. sub_match类型的对象只能通过对match_results类型的对象取下标获取
b. sub_match类型的对象可以和std:basic_string或const char*的字符串进行比较
c. sub_match类型的对象可以和std::basic_string或const char*的字符串相加,生成新的std::basic_string类型的字符串
sub_match所提供的接口请参考:http://www.boost.org/doc/libs/1_37_0/libs/regex/doc/html/boost_regex/ref/sub_match.html

(4)reg_match, reg_search和reg_replace
reg_match, reg_search和reg_replace都是Boost.Regex所提供的具体进行正则匹配的算法接口。
reg_match用来判定整个字符串是否匹配指定的的正则表达式, 具体定义参见:http://www.boost.org/doc/libs/1_37_0/libs/regex/doc/html/boost_regex/ref/regex_match.html
reg_search用来判定字符串的某一部分是否匹配指定的正则表达式, 具体定义参见:http://www.boost.org/doc/libs/1_37_0/libs/regex/doc/html/boost_regex/ref/regex_search.html
reg_replace用来把字符串中匹配指定正则表达式的部分替换为指定内容输出,对于不匹配的部分原样输出, 具体定义参见:http://www.boost.org/doc/libs/1_37_0/libs/regex/doc/html/boost_regex/ref/regex_replace.html

#include <string>   
#include <iostream>   
#include "boost/regex.hpp"   

int main(int argc, char ** argv)   
{   
    if (argc != 4)   
    {      
        std::cerr<<"Usage: " << argv[0] <<" option regex text\n"   
                <<" option: 0 --whole match\n"   
                <<"        1 --sub match\n"   
                <<"        2 --replace match\n";   
        return 1;   
    }      
    
    boost::regex oRegex(argv[2]);   
    boost::smatch oResults;   
    std::string strStr(argv[3]);   
    std::string strRes;   
    
    switch (atoi(argv[1]))   
    {      
    case 0:   
        if(boost::regex_match(strStr, oResults, oRegex))   
        {      
            std::cout << strStr << " matches " << oRegex << "\n";   
        }      
        else   
        {      
            std::cout << strStr << " doesn't match " << oRegex << "\n";   
        }      
        break;   
    case 1:   
        if(boost::regex_search(strStr, oResults, oRegex))   
        {   
            std::cout << strStr << " matches " << oRegex << "\n";   
        }   
        else   
        {   
            std::cout << strStr << " doesn't match " << oRegex << "\n";   
        }   
        break;   
    case 2:   
        strRes = boost::regex_replace(strStr, oRegex, "");   
        std::cout << "strRes=" << strRes << "\n";   
        break;   
    default:   
        std::cerr << "Invalid option: " << argv[1] << "\n";   
        break;   
    }   
}

用法

要使用Boost.Regex, 你需要包含头文件"boost/regex.hpp". Regex是本书中两个需要独立编译的库之一(另一个是Boost.Signals)。你会很高兴获知如果你已经构建了Boost— —那只需在命令提示符下打一行命令——就可以自动链接了(对于Windows下的编译器),所以你不需要为指出那些库文件要用而费心。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值