ip地址的正则表达式

"((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)){1}";

/**
* Created by baiwenlong on 11/26/15.
*/
public class IpUtils {

private static final String sIpPatternStr = "((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)){1}";
private static Pattern sIpPattern = Pattern.compile(sIpPatternStr);//ip的正则表达式

/**
* 判断url的domain是否为ip地址(http:255.255.255.255/xxxx/a?a=a
* @param url
* @return
*/
public static boolean isWithIpDomain(String url) {
if (url == null) {
return false;
}
try {
String domain = new URL(url).getHost();
return isIp(domain);
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
}

/**
* 是否是ip地址(255.255.255.255)
* @param ipStr
* @return
*/
public static boolean isIp(String ipStr) {
if (ipStr == null) {
return false;
}
return sIpPattern.matcher(ipStr).matches();
}
 
 

posted on 2015-11-30 14:58  bwlcool  阅读(1079)  评论(0编辑  收藏  举报

导航