is string a Valid IP Address - Android java.util.regex

Android examples for java.util.regex:IP Address Pattern

Description

is string a Valid IP Address

Demo Code

import java.text.DateFormat;
import java.text.ParseException;

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

public class Main{

    public static boolean isIPAddressValid(String IPAddress) {
        boolean isValid = false;
        Pattern pattern = Pattern
                .compile("\\b((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\b");
        Matcher matcher = pattern.matcher(IPAddress);
        if (matcher.matches()) {
            isValid = true;/*from   ww w.  j  a va2 s .c  o m*/
        }
        return isValid;

    }

}

Related Tutorials