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