A Panda
A Panda

Reputation: 13

java Regular Expression- Required String validation

I need a regex which check a string and return true if all conditions are satisfied.

Conditions:

  1. Length of the string 8 character.

  2. The string contains 2 lowercase letter, 4 uppercase letter and 2 Symbols (#, -).

  3. All the symbol or character can be any where in the string.

My Junit TestCase

package com.ap;

import static org.junit.Assert.assertTrue;
import org.junit.Test;

    public class RegularExpressionTest {

    private static RegularExpression re = new RegularExpression();

    @Test
    public void correctInput1() {
        boolean result = re.check("abABCD#-");  
        assertTrue(result);     
    }

    @Test
    public void correctInput2() {
        boolean result = re.check("bABCD#-a");  
        assertTrue(result);     
    }

    @Test
    public void correctInput3() {
        boolean result = re.check("bABCD#-a");  
        assertTrue(result);     
    }

    @Test
    public void correctInput4() {
        boolean result = re.check("ABCD#-ab");  
        assertTrue(result);     
    }

    @Test
    public void correctInput5() {
        boolean result = re.check("CD#-abAB");  
        assertTrue(result);     
    }

    @Test
    public void correctInput6() {
        boolean result = re.check("#aABb-CD");  
        assertTrue(result);     
    }

    @Test
    public void correctInput7() {
        boolean result = re.check("-A#bDaBC");  
        assertTrue(result);     
    }

    @Test
    public void incorrectInput1() {
        boolean result = re.check("abABC#-");   //total 7 character
        assertTrue(result);     
    }
    @Test
    public void incorrectInput2() {
        boolean result = re.check("abABCDE#-"); //total 9 character
        assertTrue(result);     
    }

    @Test
    public void incorrectInput3() {
        boolean result = re.check("abABCDE#");  // "-" symbol absent 
        assertTrue(result);     
    }

    @Test
    public void incorrectInput4() {
        boolean result = re.check("abABCDEFG"); // Symbol absent
        assertTrue(result);     
    }

    @Test
    public void incorrectInput5() {
        boolean result = re.check("ABCDEEF#-"); //lower case letter absent
        assertTrue(result);     
    }

    @Test
    public void incorrectInput6() {
        boolean result = re.check("abcdef#-");  // Upper case letter absent
        assertTrue(result);     
    }

    @Test
    public void incorrectInput7() {
        boolean result = re.check("abcABCf#-");//4 Uppercase & 2 lowercase character
        assertTrue(result);     
    }


}

Upvotes: 0

Views: 103

Answers (1)

ndnenkov
ndnenkov

Reputation: 36110

(?=(?:.*[a-z]){2})(?=(?:.*[A-Z]){4})(?=(?:.*[-#]){2})^.{8}$

See it in action


The general idea:

  • (?:.*x){n} - matches if the string has at least n xes
  • (?=...) - multiple lookaheads can be concatenated to check for multiple properties of the string
  • ^.{n}$ - from the start of the string til the end of the string there are exactly n characters.

Upvotes: 2

Related Questions