Ip Address Validator Regular Expression Pattern
for more regular expression syntax refer Java Regular Expression Syntax Descriptions
IpAddressValidator .java
Output :
(?:(?: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
0 comments:
Post a Comment