Java -Regular Expression -Username-Password-Email-IPAddress-validation-example

Package java.util.regex 


Classes for matching character sequences against patterns specified by regular expressions.An instance of the Pattern class represents a regular expression that is specified in string form in a syntax similar to that used by Perl.
Instances of the Matcher class are used to match character sequences against a given pattern. Input is provided to matchers via the CharSequence interface in order to support matching against characters from a wide variety of input sources.Unless otherwise noted, passing a null argument to a method in any class or interface in this package will cause a NullPointerException to be thrown.

Username Regular Expression:


Password Regular Expression:


Email Regular Expression:


IP address Regular Expression:



Validator Class

package com.knowledgefactory.regularexpressions;

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

public class Validator {

private static Pattern pattern;
private static Matcher matcher;
private static final String PASSWORD_PATTERN = "((?=.*[a-z])(?=.*\\d)(?=.*[A-Z])(?=.*[@#$%!]).{8,40})";
private static final String USER_NAME_PATTERN = "^[a-zA-Z0-9._-]{3,}$";
private static final String EMAIL_PATTERN = "^[\\w!#$%&'*+/=?`{|}~^-]+(?:\\.[\\w!#$%&'*+/=?`{|}~^-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$";
private static final String IP_PATTERN = "^((0|1\\d?\\d?|2[0-4]?\\d?|25[0-5]?|[3-9]\\d?)\\.){3}(0|1\\d?\\d?|2[0-4]?\\d?|25[0-5]?|[3-9]\\d?)$";

// Password validation
public boolean isStrongPassword(final String password) {
pattern = Pattern.compile(PASSWORD_PATTERN);
matcher = pattern.matcher(password);
return matcher.matches();


// User name validation
public boolean isUserName(final String username) {
pattern = Pattern.compile(USER_NAME_PATTERN);
matcher = pattern.matcher(username);
return matcher.matches();


// Email validation
public boolean isEmail(final String email) {
pattern = Pattern.compile(EMAIL_PATTERN);
matcher = pattern.matcher(email);
return matcher.matches();


// IP validation
public boolean isIP(final String IPaddress) {
pattern = Pattern.compile(IP_PATTERN);
matcher = pattern.matcher(IPaddress);
return matcher.matches();


TestMain Class

package com.knowledgefactory.regularexpressions;

public class RegexTest {

public static void main(String[] args) {

  Validator obj = new Validator();
  // Password validation
final String strong_pass = "KnowledgeFactory#123";
final String weak_pass = "123";
System.out.println(strong_pass + " is Strong:  " + obj.isStrongPassword(strong_pass));
System.out.println(weak_pass + " is Strong?:  " + obj.isStrongPassword(weak_pass));

// User name validation
final String valid_username = "Sibin";
final String invalid_username = "123%";
System.out.println(valid_username + " is Valid:  " + obj.isUserName(valid_username));
System.out.println(invalid_username + " is Valid:  " + obj.isUserName(invalid_username));

// Eamil validation
final String valid_email = "";
final String invalid_email = "";
System.out.println(valid_email + " is Valid:  " + obj.isEmail(valid_email));
System.out.println(invalid_email + " is Valid:  " + obj.isEmail(invalid_email));

// Ip validation
final String valid_Ip = "";
final String invalid_Ip = "100.1.3.";
System.out.println(valid_Ip + " is Valid:  " + obj.isIP(valid_Ip));
System.out.println(invalid_Ip + " is Valid:  " + obj.isIP(invalid_Ip));



KnowledgeFactory#123 is Strong:  true
123 is Strong?:  false
Sibin is Valid:  true
123% is Valid:  false is Valid:  true is Valid:  false is Valid:  true
100.1.3. is Valid:  false

This article is contributed by Sibin. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above