How do you validate that a string is a valid IPv4 address in C++?

前端 未结 17 2051
悲哀的现实
悲哀的现实 2020-12-04 23:21

I don\'t need to validate that the IP address is reachable or anything like that. I just want to validate that the string is in dotted-quad (xxx.xxx.xxx.xxx) IPv4 format, w

17条回答
  •  情深已故
    2020-12-05 00:18

    Boost.Regex would be appropriate.

    bool validate_ip_address(const std::string& s)
    {
       static const boost::regex e("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
       return regex_match(s, e);
    }
    

提交回复
热议问题