(四)boost库之正则表达式regex

正则表达式可以为我们带来极大的方便,有了它,再也不用为此烦恼

头文件:

#include <boost/regex.hpp>

1、完全匹配

    std::string str("abcd");
    boost::regex reg( "a\\w*d" );
    if (regex_match(str, reg))
    {
        std::cout << str << " is match" << std::endl;
    }
    else
    {
        std::cout << str << " is not match" << std::endl;
    }

2、完全匹配并获取子串

    const char* mail = "tengxun@qq.com";
    boost::cmatch res;
    //建立3个子表达式
    boost::regex reg("(\\w+)@(\\w+).(\\w+)");
    if (boost::regex_match(mail,res, reg))
    {
        //既可以通过迭代器获取数据, 也可以通过数组方式获取数据
        for (boost::cmatch::iterator pos = res.begin(); pos != res.end(); ++pos)
        {
            std::cout << *pos << std::endl;
        }
        //res[0]存放匹配到的完整字符串
        std::cout << "name:" << res[1] << std::endl;
    }

 

3、查找, 当你不需要匹配整个字符串的时候,可以选择查找

    const char* mail = "tengxun@qq.com.cn";
    boost::cmatch res;
    //建立3个子表达式
    boost::regex reg("(\\w+)@(\\w+).(\\w+)");
    if (boost::regex_search(mail,res, reg))
    {
        std::cout <<"**************************************" << std::endl;
        //既可以通过迭代器获取数据, 也可以通过数组方式获取数据
        for (boost::cmatch::iterator pos = res.begin(); pos != res.end(); ++pos)
        {
            std::cout << *pos << std::endl;
        }
        //res[0]存放匹配到的完整字符串
        std::cout << "match :" << res[0] << std::endl << "name:" << res[1] << std::endl;
    }

4、替换

替换匹配到的子字符串, 可以通过$N 引用第N个匹配到的值、$&  引用全匹配

#include <boost/algorithm/string.hpp>
void TestReplace()
{
    //将tengxun@qq.com.cn 替换成tengxun@139.com.cn
    std::string mail("tengxun@qq.com.cn");
    //建立3个子表达式
    boost::regex reg("(\\w+)@(\\w+).(\\w+)");
    std::cout << boost::regex_replace(mail, reg, "$1@139.$3") << std::endl;
    std::cout << boost::regex_replace(mail, reg, "my$1@$2.$3") << std::endl;
    //自定义替换函数,regex_replace将匹配到的字符串数组传递给回调函数,由回调函数返回新的字符串
    std::cout << boost::regex_replace(mail, reg, [](const boost::smatch  &m){
        return boost::to_upper_copy(m[0].str());
    });
}

5、迭代

当需要从字符串中提取多个表达式时,可以采用迭代进行提取

    std::string str("tengxun@qq.com, aa@tt.com, bb@qq.com");
    boost::regex reg("(\\w+)@(\\w+).(\\w+)");
    boost::sregex_iterator pos(str.begin(), str.end(), reg);
    boost::sregex_iterator end;
    while(pos != end)
    {
        std::cout << "[" << (*pos)[0] << "]";
        ++pos;
    }

 

6、分词

#include <iostream>
#include <boost/regex.hpp>
void TestToken()
{
    using namespace std;
    using namespace boost;
    string str("tengxun@qq.com, aa@tt.com, bb@qq.com");
    regex reg("\\w+");
    sregex_token_iterator pos(str.begin(), str.end(), reg);
    while(pos != sregex_token_iterator())
    {
        cout << "[" << *pos << "]" ;
        ++pos;
    }
    cout << endl;
    //如果最后一个参数args为-1,则把匹配到的字符串视为分隔符
    regex split_reg(",");
    pos = sregex_token_iterator(str.begin(), str.end(), split_reg, -1);
    while(pos != sregex_token_iterator())
    {
        cout << "[" << *pos << "]" ;
        ++pos;
    }
    cout << endl;
    //如果最后一个参数args为正数,则返回匹配结果的第args个子串
    regex split_sub_reg("(\\w*)@(\\w*).(\\w*)");
    pos = sregex_token_iterator(str.begin(), str.end(), split_sub_reg, 1);
    while(pos != sregex_token_iterator())
    {
        cout << "[" << *pos << "]" ;
        ++pos;
    }
    cout << endl;
    //匹配并指定输出顺序
    //从下面字符串中提取日期,并转换成 年月日 的顺序输出
    std::string input("01/02/2003 blahblah 04/23/1999 blahblah 11/13/1981");
    regex re("(\\d{2})/(\\d{2})/(\\d{4})"); // find a date
    int const sub_matches[] = { 3, 1, 2 }; // year,month, day
    sregex_token_iterator begin( input.begin(), input.end(), re, sub_matches ), end;
    // write all the words to std::cout
    std::ostream_iterator< std::string > out_iter( std::cout, "\n" );
    std::copy( begin, end, out_iter );
}

转载于:https://my.oschina.net/lingluonianhua/blog/211788

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值