is Valid IP Address by regex - Java java.util.regex

Java examples for java.util.regex:Match IP Address

Description

is Valid IP Address by regex

Demo Code


//package com.java2s;

import java.util.regex.Pattern;

public class Main {
    public static void main(String[] argv) throws Exception {
        String address = "java2s.com";
        System.out.println(isValidAddress(address));
    }/*from w  w  w  .j av a2  s  .c  o  m*/

    private static final String IPADDRESS_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])$";

    public static boolean isValidAddress(String address) {
        if (address == null) {
            return false;
        }

        return Pattern.compile(IPADDRESS_PATTERN).matcher(address)
                .matches();
    }
}

Related Tutorials