STL 正则表达式-regex运用

// Algorithm.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "MainFrame.h"
#include 
#include 
#include 
using namespace std;

int main()
{

    regex rTime("(?:[0]?[0-9]|1[0-9]|2[0-3])(:(?:0?[0-9]|[1-5][0-9])){2}");
    //输入时间,判断是否满足格式(00:00:00)
    string str;
    while (true)
    {
        cin >> str;
        if (str == "q") break;

        if (regex_match(str, rTime))
        {
            cout << "the time : " << str << endl;
        }
        else
        {
            cout << "Invalid date!" << endl;
        }
    }

    //匹配一个或者多个字母切结尾为day
    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;

    cout << "-------------regex_search-------" << endl;
    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 << "s2匹配结果:" << rr2.str() << endl;
    cout << endl;

    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 << 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;
    }
    cout << endl;

    //匹配IP
    regex reg2("(\\d{1,3}):(\\d{1,3}):(\\d{1,3}):(\\d{1,3})");
    string ip = "192:168:1:1";
    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;

    cout << 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;
}








你可能感兴趣的:(C++,STL)