C++11中正则表达式测试

来源:互联网 发布:centos网络配置命令 编辑:程序博客网 时间:2024/05/17 22:45

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