求一个能验证端口号的正则表达式,要求能在eclipse中使用。
2个回答
展开全部
public static int getPort(String url) {
String re="(([a-z][a-z0-9]*)://)?([^@:]+:[^@]*@)?([^\\s\\?\\.:&]+(\\.[^\\s\\?\\.:&]+)*)(:(\\d+))?(/.*)?";
Matcher matcher = Pattern.compile(re, Pattern.CASE_INSENSITIVE).matcher(url);
String protocol = matcher.group(2);
String port = matcher.group(7);
if (port == null) {
if (protocol == null || "http".equalsIgnoreCase(protocol))
port = "80";
else if ("ftp".equalsIgnoreCase(protocol))
port = "21";
}
return port == null ? -1 : Integer.parseInt(port);
}
String re="(([a-z][a-z0-9]*)://)?([^@:]+:[^@]*@)?([^\\s\\?\\.:&]+(\\.[^\\s\\?\\.:&]+)*)(:(\\d+))?(/.*)?";
Matcher matcher = Pattern.compile(re, Pattern.CASE_INSENSITIVE).matcher(url);
String protocol = matcher.group(2);
String port = matcher.group(7);
if (port == null) {
if (protocol == null || "http".equalsIgnoreCase(protocol))
port = "80";
else if ("ftp".equalsIgnoreCase(protocol))
port = "21";
}
return port == null ? -1 : Integer.parseInt(port);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询