// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.util.regex.*;

public class RegexIp {
    public static void main(String[] args) {
        System.out.println(isValidIp("1.2.3.4"));
        System.out.println(isValidIp("a.b.c.d"));
        System.out.println(isValidIp2("1.2.3.4"));
        System.out.println(isValidIp2("a.b.c.d"));
    }
    private static Pattern p = Pattern.compile("[0-9]+[.][0-9]+[.][0-9]+[.][0-9]+");
    public static boolean isValidIp(String s) {
        return p.matcher(s).matches();
    }
    public static boolean isValidIp2(String s) {
        try {
            String[] parts = s.split("[.]");
            int v1 = Integer.parseInt(parts[0]);
            int v2 = Integer.parseInt(parts[1]);
            int v3 = Integer.parseInt(parts[2]);
            int v4 = Integer.parseInt(parts[3]);
            return (v1 >= 0 && v1 <= 255 &&
                    v2 >= 0 && v2 <= 255 &&
                    v3 >= 0 && v3 <= 255 &&
                    v4 >= 0 && v4 <= 255);
        } catch(Exception e) {
            return false;
        }
    }
}
