Friday 22 December 2017

How to write a regex to validate the IP Address | Regex in java


Click here to watch on Youtube :
https://www.youtube.com/watch?v=wLzQTdd_6Mo&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
How to write a regex to validate the IP Address | Regex in java
IPAddressValidator.java

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

/**
 *
 * How to validate IP address with regular expression
 *
 */

public class IPAddressValidator
{

    private Pattern pattern;
    private Matcher matcher;

    private static final String IP_ADDRESS_REGEX = "^([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 IPAddressValidator()
    {
        pattern = Pattern.compile(IP_ADDRESS_REGEX);
    }

    /**
     * Validate ipAddress with regular expression
     *
     * @param ipAddress  ip address for validation
     * @return true valid ip address, false invalid ip address
     */

    public boolean validate(final String ipAddress)
    {
        matcher = pattern.matcher(ipAddress);
        return matcher.matches();
    }
}

RegexDemo.java

import java.util.Arrays;
import java.util.List;

/**
 *
 * How to validate IP address with regular expression
 *
 */

public class RegexDemo
{

    public static void main(String[] args)
    {
        List<String> validIPList = Arrays.asList("1.1.1.1", "255.255.255.255",
                "192.168.1.2", "10.10.1.1", "132.253.111.10", "26.10.2.10",
                "127.0.0.1");

        IPAddressValidator ipAddressValidator = new IPAddressValidator();

       
        for (String ipAddress : validIPList)
        {
            System.out.println(ipAddress + " is Valid? = "
                    + ipAddressValidator.validate(ipAddress));
        }

        System.out.println("--------------------------------------");

        List<String> inValidIPList = Arrays.asList("10.10.10", "222.222.2.999",
                "10.0.0.a");
       
        for (String ipAddress : inValidIPList)
        {
            System.out.println(ipAddress + " is Valid? = "
                    + ipAddressValidator.validate(ipAddress));
        }
    }

}

Output

1.1.1.1 is Valid? = true
255.255.255.255 is Valid? = true
192.168.1.2 is Valid? = true
10.10.1.1 is Valid? = true
132.253.111.10 is Valid? = true
26.10.2.10 is Valid? = true
127.0.0.1 is Valid? = true
--------------------------------------
10.10.10 is Valid? = false
222.222.2.999 is Valid? = false
10.0.0.a is Valid? = false

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/RegexDemo_ipaddress.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/RegexDemo_ipaddress

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/576579fab6c376d5ecd7cb5b7402b7fd1a7c4ae3/BasicJava/RegexDemo_ipaddress/?at=master

See also:
  • All JavaEE Videos Playlist
  • All JavaEE Videos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment