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

Validate IP Address with Port 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]?):(\\d{1,5})
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]?):(\\d{1,5})";

 public static void main(String[] args) {

  System.out.println("Validate  192.1.1.1:8 "+validate("192.1.1.1:8"));
  System.out.println("Validate  192.10.10.10:80 "+validate("192.10.10.10:80"));
  System.out.println("Validate 192.100.100.100:443 "+validate("192.100.100.100:443"));
  System.out.println("Validate 255.255.255.255:8080"+validate("255.255.255.255:8080"));
  System.out.println("Validate 255.255.255.255:80800"+validate("255.255.255.255:80800"));
  System.out.println("Validate 255.255.255.255:808000"+validate("255.255.255.255:808000"));
  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"));

 }

 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:8 trueValidate  192.10.10.10:80 trueValidate 192.100.100.100:443 trueValidate 255.255.255.255:8080trueValidate 255.255.255.255:80800trueValidate 255.255.255.255:808000falseValidate 255.255.255.256: falseValidate 255.255.256.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 with Port using Java Regular Expression Rating: 5 Reviewed By: eHowToNow