C++正则表达式校验IP地址


工作中遇到的一个bug:web前台下发非ip地址导致web退出登录

增加后台对ip地址的合法性校验

 1 #include <regex>
 2 static bool IsIp(const char *ip) {
 3     if (ip == NULL) {
 4         return false;
 5     }
 6     std::regex pattern("(?=(\\b|\\D))(((\\d{1,2})|(1\\d{1,2})|(2[0-4]\\d)|(25[0-5]))\\.){3}((\\d{1,2})|(1\\d{1,2})|(2[0-4]\\d)|(25[0-5]))(?=(\\b|\\D))");
 7     if (std::regex_match(ip, pattern)) {
 8         return true;
 9     }
10     return false;
11 }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM