C++11中正則表達式測试

VC++2010已经支持regex了, 能够用来编译下述代码.

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

/* 測试C++11中的正則表達式. */
int main()
{
    //定义正則表達式,匹配时间格式
    regex testRegex("[0-9]{2}:[0-9]{2}:[0-9]{2}\\.[0-9]{3}"); 

    //要匹配的字符串
    string strText("OTA LOG SFTCH/MPA Stream 2/Reservation Accept  07:23:50.580 Channel: 147, Pilot PN: 232"); 

    cmatch result; //结果

    //search 是匹配子字符串, match 是匹配整个字符串
    if (regex_search(strText.c_str(), result, testRegex, regex_constants::format_default))
    {
        cout << result.str() << endl;
    }
    else
    {
        cout << "fail." << endl;   
    }
}


posted @ 2014-07-17 20:29  hrhguanli  阅读(145)  评论(0编辑  收藏  举报