Java正则表达式匹配IP地址

以下代码对Ip做简单的匹配,不是完全和IP的规则一致的,尚需改进:

public static void main(String[] args) {
	String str = "192.168.1.1";
	String regex = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}";
	Pattern p = Pattern.compile(regex);
	Matcher m = p.matcher(str);
	while (m.find()) {
		System.out.println(m.group());
	}		
}


你可能感兴趣的:(Java正则表达式匹配IP地址)