【IP网址正则表达式匹配】java,IPv4网址正则表达式匹配
参考链接:
https://blog.csdn.net/weixin_39370315/article/details/126141872?ops_request_misc=%257B%2522request%255Fid%2522%253A%252256555201-0570-4C72-BD8A-DDAC115282D3%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=56555201-0570-4C72-BD8A-DDAC115282D3&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-6-126141872-null-null.142^v100^pc_search_result_base6&utm_term=IP%E5%9C%B0%E5%9D%80%E6%A0%A1%E9%AA%8C%20%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F&spm=1018.2226.3001.4187
正则表达式:
public boolean isIpValid() {
String match = "((25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}(25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))";
return ip.matches(match);
}
/**
* Description: IP网段校验
*/
public boolean isIpSegmentValid() {
String match = "((25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}(0/24)";
return ip.matches(match);
}
调用方法:
String ip = "127.0.0.1";
IP.isIpValid();