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]?)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
0 comments:
Post a Comment