文章简介
没找到完整的正则表达式,着手写一个能用的。
代码如下
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class portTest {
public static void main(String[] args) {
System.out.println(isPort("1521"));//true
System.out.println(isPort("abc"));//false
System.out.println(isPort("123456"));//false
System.out.println(isPort("65536"));//false
}
/**
* 判断端口号是否合法
*
* @param portStr
* @return
*/
public static boolean isPort(String portStr) {
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(portStr);
if (isNum.matches() && portStr.length() < 6 && Integer.valueOf(portStr) >= 1
&& Integer.valueOf(portStr) <= 65535) {
return true;
}
return false;
}
}