Showing posts with label fqdn. Show all posts
Showing posts with label fqdn. Show all posts

Sunday, February 24, 2013

validating fqdn url email hostname using java regular expression

Using java Pattern class to validate the FQDN, URL, E-Mail address hostname


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

public class Validator 
{
    private static final Pattern emailPattern = Pattern.compile("^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Z]{2,4}$", Pattern.CASE_INSENSITIVE);


    private static final Pattern fqdnPattern = Pattern.compile("(?=^.{1,254}$)(^(?:(?!\\d+\\.|-)[a-zA-Z0-9_\\-]{1,63}(?<!-)\\.?)+(?:[a-zA-Z]{2,})$)", Pattern.CASE_INSENSITIVE);

     private static final Pattern hostPattern = Pattern.compile("^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\\-]*[A-Za-z0-9])$", Pattern.CASE_INSENSITIVE);

    private static final Pattern urlPattern = Pattern.compile("[^(http\\:\\/\\/[a-zA-Z0-9_\\-]+(?:\\.[a-zA-Z0-9_\\-]+)*\\.[a-zA-Z]{2,4}(?:\\/[a-zA-Z0-9_]+)*(?:\\/[a-zA-Z0-9_]+\\.[a-zA-Z]{2,4}(?:\\?[a-zA-Z0-9_]+\\=[a-zA-Z0-9_]+)?)?(?:\\&[a-zA-Z0-9_]+\\=[a-zA-Z0-9_]+)*)$]", Pattern.CASE_INSENSITIVE);


    public static boolean isInteger(String intVal)
    {
        try {
            int i = new Integer(intVal);
            return true;
        } catch (Exception e) {
        }
        return false;
    }

    public static boolean isBoolean(String boolVal)
    {
        try {
            boolean b = new Boolean(boolVal);
            return true;
        } catch (Exception e) {
        }
        return false;
    }

    public static boolean isFloat(String floatVal)
    {
        try {
            float f = new Float(floatVal);
            return true;
        } catch (Exception e) {
        }
        return false;
    }

    public static boolean isLong(String logVal)
    {
        try {
            long l = new Long(logVal);
            return true;
        } catch(Exception e) {
        }
        return false;
    }

    public static boolean isFQDN(String fqdnVal)
    {
        try {
                   Matcher matcher = fqdnPattern.matcher(fqdnVal); 
                return matcher.matches(); 
        } catch (Exception e){
        }
        return false;


    }

    public static boolean isURL(String url)
    {
        try {
                   Matcher matcher = urlPattern.matcher(url); 
                return matcher.matches(); 
        } catch (Exception e){
        }
        return false;

    }

    public static boolean isEmailAddr(String emailAddr)
    {
        try {
                   Matcher matcher = emailPattern.matcher(emailAddr); 
                return matcher.matches(); 
        } catch (Exception e){
        }
        return false;
    }

    public static boolean isHost (String hostname)
    {
        try {
                   Matcher matcher = hostPattern.matcher(hostname); 
                return matcher.matches(); 
        } catch (Exception e){
        }
        return false;
    }

    public static void main(String args[])
    {
        try {
            String action = args[0];
            String input = args[1];
            boolean result = false;
            if (action.equals("email")) {
                result = isEmailAddr(input);   
            } else if (action.equals("host")) {
                result = isHost(input);
            } else if(action.equals("url")) {
                result = isURL(input);
            } else if(action.equals("fqdn")) {
                result = isFQDN(input);
            }
            System.out.println("RESULT : " + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}