xpressive是boost的正则表达式库

xpressive是boost的正则表达式库,它比boost.regex要好的是它不需要编译,速度快。 

查找char*

#include<boost/xpressive/xpressive_dynamic.hpp>
#include<iostream>
using namespace std;
int main()
{
    using namespace boost::xpressive;
    char* str = "there is a POWER-suit item";
    cregex reg = cregex::compile("(power)-(.{4})",icase);//icase:忽略大小写
    assert(regex_search(str,reg));
    cmatch what;
    regex_search(str,what,reg);
    assert(what.size()==3);
    cout << what[0] << "," << what[1] << "," << what[2] << endl;
}
POWER-suit,POWER,suit


匹配(string) 
#include<boost/xpressive/xpressive_dynamic.hpp>
#include<iostream>
using namespace std;
int main()
{
    using namespace boost::xpressive;
    string str("Power-bomb,power-suit,pOWER-beam all items\n");
    sregex reg = sregex::compile("power-(\\w{4})",icase);
    sregex_iterator pos(str.begin(),str.end(),reg);
    sregex_iterator end;
    while(pos!=end)
    {
        cout << (*pos)[0] << " ";
        ++pos;
    }
    cout << endl;
}
Power-bomb power-suit pOWER-beam 

匹配:

#include<boost/xpressive/xpressive_dynamic.hpp>
#include<iostream>
using namespace std;
int main()
{
    using namespace boost::xpressive;
    string str("readme.txt");
    sregex reg1 = sregex::compile("(.*)(me)");
    sregex reg2 = sregex::compile("(t)(.)(t)");
    cout << regex_replace(str,reg1,"manual") << endl;
    cout << regex_replace(str,reg1,"$1you") << endl;//$:匹配行的末尾 readyou
    cout << regex_replace(str,reg1,"$&$&") << endl;
    cout << regex_replace(str,reg2,"$1N$3") << endl;
    str = regex_replace(str,reg2,"$1$3");
    cout << str << endl;
}

manual.txt
readyou.txt
readmereadme.txt
readme.tNt
readme.tt

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值