你的位置:首页 > Java教程

[Java教程]正则表达式在Java中验证IP


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());  }}