以下代码对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);
return m.matches();
while (m.find()) {
System.out.println(m.group());
}
} |
|