每个ip数值为0-225 否则输出false,正确返回true
例如:输入:
000.12.12.034
121.234.12.12
23.45.12.56
00.12.123.123123.123
122.23
Hello.IP
输出为:
true true true false false false
import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.Scanner; class Solution{ public static void main(String []args){ Scanner in = new Scanner(System.in); while(in.hasNext()){ String IP = in.next(); System.out.println(IP.matches(new MyRegex().pattern)); } } } class MyRegex { String pattern = "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$"; }
原文:http://www.cnblogs.com/Angella/p/6536314.html