boost regex正则区配 IP 地址

来源:互联网 发布:维塔斯的奉献知乎 编辑:程序博客网 时间:2024/05/21 12:48
#include <boost/regex.hpp>#include <iostream>#include <string>int main(){    std::string text(" 192.168.0.1 abc 10.0.0.255 10.5.1 1.2.3.4a 5.4.3.2 ");    const char* pattern =        "\\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"        "\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"        "\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"        "\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b";    boost::regex ip_regex(pattern);    boost::sregex_iterator it(text.begin(), text.end(), ip_regex);    boost::sregex_iterator end;    for (; it != end; ++it) {        std::cout << it->str() << "\n";        // v.push_back(it->str()); or something similar         }}

输出:

192.168.0.110.0.0.2555.4.3.2

参考:http://stackoverflow.com/questions/5804453/c-regular-expressions-with-boost-regex

原创粉丝点击