c++11中正则表达式的使用

环境:Ubuntu 15.10       QtCreator     CMake      c++


头文件  #include

regex_search:

std::regex pattern;
pattern = "(HTTP/1\\.1|HTTP/1\\.0) \\d{3} [ \\-a-zA-Z]+\r\n(.+: .+\r\n)+\r\n";
    test = "HTTP/1.1 200 OK\r\n"
           "Date: Thu, 24 Dec 2015 15:25:43 GMT\r\n"
           "Server: Apache/2.2.15 (CentOS)\r\n"
           "X-Powered-By: PHP/5.3.3\r\n"
           "Set-Cookie: PHPSESSID=ioa4vje3mm8t3utigst4rjgdk4; path=/\r\n"
           "Expires: Thu, 19 Nov 1981 08:52:00 GMT\r\n"
           "Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0\r\n"
           "Pragma: no-cache\r\n"
           "Content-Length: 22\r\n"
           "Connection: close\r\n"
           "Content-Type: text/html; charset=utf-8\r\n"
           "\r\n"
            "cannot connect server\r\n";

if(regex_search(test, pattern))
        cout << "successful" << endl;
    else
        cout << "failed" << endl;

regex_match:

std::regex pattern;
pattern = "^((\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])\\.){3}(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])$";
    test = "123.123.123.123";

if(regex_search(test, pattern))
        cout << "successful" << endl;
    else
        cout << "failed" << endl;

在使用过程中遇到的问题为换行符(\r\n)的匹配,找了一圈并没有找到有单行模式的选项,最后只能用regex_search来代替 regex_match的使用。


你可能感兴趣的:(c++)