• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

【正则表达式1】C++11正则表达式

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

头文件

#include <regex>

regex_match:整个字符串是否匹配

    regex reg1("\\w+day");
    string s1 = "saturday";
    string s2 = "saturday and sunday";
    smatch r1;
    smatch r2;
    cout << boolalpha << regex_match(s1, r1, reg1) << endl;  //true
    cout << boolalpha << regex_match(s2, r2, reg1) << endl;  //false
    cout << "s1匹配结果:" << r1.str() << endl;          //saturday
    cout << "s2匹配结果:" << r2.str() << endl;          //空
    cout << endl;

regex_match:只返回第一个匹配结果

    smatch rr1;
    smatch rr2;
    cout << boolalpha << regex_search(s1, rr1, reg1) << endl;  //true
    cout << "s1匹配结果:" << rr1.str() << endl;           //saturday
    cout << boolalpha << regex_search(s2, rr2, reg1) << endl;  //true
    cout << "s1匹配结果:" << rr2.str() << endl;           //saturday
    cout << endl;

iterator:返回多个匹配结果

类似于指针,调用成员要用"->"

    cout << "iterator结果:" << endl;
    sregex_iterator it(s2.begin(), s2.end(), reg1);
    sregex_iterator end;
    for(; it != end; ++it)
    {
        cout << it->str() << endl;
        //cout << *it << endl; 错误
    }

    cout << "token_iterator结果:" << endl;
    sregex_token_iterator tit(s2.begin(), s2.end(), reg1);
    sregex_token_iterator tend;
    for(; tit != tend; ++tit)
    {
        cout << tit->str() << endl;
        cout << *tit << endl;
    }

 

子表达式匹配

    regex reg2("(\\d{1,3}):(\\d{1,3}):(\\d{1,3}):(\\d{1,3})");
    string ip = "0:11:222:333";
    smatch m; 
    regex_match(ip, m, reg2);
    cout << "输出:str()" << endl;
    cout << m.str() << endl;   //0:11:222:333
    cout << m.str(1) << endl;  //0
    cout << m.str(2) << endl;  //11
    cout << m.str(3) << endl;  //222
    cout << m.str(4) << endl;  //333

    cout << "输出:[i]" << endl; //结果同上
    cout << m[0] << endl;
    cout << m[1] << endl;
    cout << m[2] << endl;
    cout << m[3] << endl;
    cout << m[4] << endl;

多个匹配结果

    string ip2 = "0:11:222:333 4:55:66:7";
    sregex_iterator ip_it(ip2.begin(), ip2.end(), reg2);
    sregex_iterator ip_end;
    for(; ip_it != ip_end; ++ip_it)
    {
        cout << ip_it->str() << endl;
        cout << ip_it->str(1) << endl;
        cout << ip_it->str(2) << endl;
        cout << ip_it->str(3) << endl;
        cout << ip_it->str(4) << endl;
    }

 

总的程序:

#include <iostream>
#include <string>
#include <regex>
using namespace std;

int main()
{
    //regex_match匹配整个字符串
    regex reg1("\\w+day");
    string s1 = "saturday";
    string s2 = "saturday and sunday";
    smatch r1;
    smatch r2;
    cout << boolalpha << regex_match(s1, r1, reg1) << endl;
    cout << boolalpha << regex_match(s2, r2, reg1) << endl;
    cout << "s1匹配结果:" << r1.str() << endl;
    cout << "s2匹配结果:" << r2.str() << endl;
    cout << endl;

    //regex_match只返回第一个匹配结果
    smatch rr1;
    smatch rr2;
    cout << boolalpha << regex_search(s1, rr1, reg1) << endl;
    cout << "s1匹配结果:" << rr1.str() << endl;
    cout << boolalpha << regex_search(s2, rr2, reg1) << endl;
    cout << "s1匹配结果:" << rr2.str() << endl;
    cout << endl;


    //使用iterator返回多个匹配结果
    //结果要用->
    cout << "iterator结果:" << endl;
    sregex_iterator it(s2.begin(), s2.end(), reg1);
    sregex_iterator end;
    for(; it != end; ++it)
    {
        cout << it->str() << endl;
        //cout << *it << endl; 错误
    }

    cout << "token_iterator结果:" << endl;
    sregex_token_iterator tit(s2.begin(), s2.end(), reg1);
    sregex_token_iterator tend;
    for(; tit != tend; ++tit)
    {
        cout << tit->str() << endl;
        cout << *tit << endl;
    }

    //子表达式匹配
    regex reg2("(\\d{1,3}):(\\d{1,3}):(\\d{1,3}):(\\d{1,3})");
    string ip = "0:11:222:333";
    smatch m; 
    regex_match(ip, m, reg2);
    cout << "输出:str()" << endl;
    cout << m.str() << endl;
    cout << m.str(1) << endl;
    cout << m.str(2) << endl;
    cout << m.str(3) << endl;
    cout << m.str(4) << endl;

    cout << "输出:[i]" << endl;
    cout << m[0] << endl;
    cout << m[1] << endl;
    cout << m[2] << endl;
    cout << m[3] << endl;
    cout << m[4] << endl;

    //输出结果同上
    //regex_search(ip, m, str2);
    cout << endl;
    string ip2 = "0:11:222:333 4:55:66:7";
    sregex_iterator ip_it(ip2.begin(), ip2.end(), reg2);
    sregex_iterator ip_end;
    for(; ip_it != ip_end; ++ip_it)
    {
        cout << ip_it->str() << endl;
        cout << ip_it->str(1) << endl;
        cout << ip_it->str(2) << endl;
        cout << ip_it->str(3) << endl;
        cout << ip_it->str(4) << endl;
    }

    return 0;

}
View Code

 

参考:

http://www.cnblogs.com/zhuyp1015/archive/2012/04/08/2438191.html

http://www.cnblogs.com/zhuyp1015/archive/2012/04/08/2438215.html


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#多线程学习(四)多线程的自动管理(线程池)发布时间:2022-07-13
下一篇:
C语言库函数实现【微软面试100题第八十三题】发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap