We are moved to new domain
Click -> www.ehowtonow.com
Sunday, 26 March 2017

Validate IP Address using Java Regular Expression

Ip Address Validator Regular Expression Pattern
(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)
Regular ExpressionDescription


for more regular expression syntax refer Java Regular Expression Syntax Descriptions 

IpAddressValidator .java
package com.javatutorialcorner.regex;

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

public class IpAddressValidator {
 private static final String PATTERN = "(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)";

 public static void main(String[] args) {

  System.out.println("Validate  192.1.1.1 "+validate("192.1.1.1"));
  System.out.println("Validate  192.10.10.10 "+validate("192.10.10.10"));
  System.out.println("Validate 192.100.100.100 "+validate("192.100.100.100"));
  System.out.println("Validate 255.255.255.255 "+validate("255.255.255.255"));
  System.out.println("Validate 255.255.255.256 "+validate("255.255.255.256"));
  System.out.println("Validate 255.255.256.255 "+validate("255.255.256.255"));
  System.out.println("Validate 255.256.255.255 "+validate("255.256.255.255"));
  System.out.println("Validate 256.255.255.255 "+validate("256.255.255.255"));
  System.out.println("Validate 25a.255.255.255 "+validate("256.255.255.255"));

 }

 private static boolean validate(String password) {
  Pattern pattern = Pattern.compile(PATTERN);
  Matcher matcher = pattern.matcher(password);
  return matcher.matches();

 }

}


Output : 
Validate  192.1.1.1 trueValidate  192.10.10.10 trueValidate 192.100.100.100 trueValidate 255.255.255.255 trueValidate 255.255.255.256 falseValidate 255.255.256.255 falseValidate 255.256.255.255 falseValidate 256.255.255.255 falseValidate 25a.255.255.255 false

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Validate IP Address using Java Regular Expression Rating: 5 Reviewed By: eHowToNow