正则表达式在Java中验证IP

12/12/2015来源:Java教程人气:2530

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * JavaString
 * com.xuyanghui.regularexPRession
 * 15-10-24
 * 下午2:31
 * YANGHUI XU
 */
public class Regular {
    public static void main(String[] args) {
        String ip = "192.168.1.108";

        String patternString = "(([0,1]?\\d?\\d|2[0-4]\\d|25[0-5])\\.){3}([0,1]?\\d?\\d|2[0-4]\\d|25[0-5])";
        Pattern pattern = Pattern.compile(patternString);
        Matcher matcher = pattern.matcher(ip);
        System.out.println(matcher.matches());


    }
}