パスワードの正規表現パターン

((?=.** \d)(?=.** [a-z])(?=.** [A-Z])(?=.** [@#$%]).{6,20})

説明

(           # Start of group
  (?=.** \d)      #   must contains one digit from 0-9
  (?=.** [a-z])       #   must contains one lowercase characters
  (?=.** [A-Z])       #   must contains one uppercase characters
  (?=.** [@#$%])      #   must contains one special symbols in the list "@#$%"
              .     #     match anything with previous condition checking
                {6,20}  #        length at least 6 characters and maximum of 20
)           # End of group


?= – アサーション条件を適用することを意味し、それは意味を持ちません。常に他の組み合わせで動作します

全体の組み合わせは、少なくとも1桁、大文字1文字、小文字1文字、特殊記号1文字( “@#$%”)を含む6〜20文字の文字列を意味します。この正規表現パターンは、強力で複雑なパスワードを実装するのに非常に便利です。

__P.Sグループ化の式の順序は関係ありません。

1. Javaの正規表現の例

PasswordValidator.java

package com.mkyong.regex;

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

public class PasswordValidator{

      private Pattern pattern;
      private Matcher matcher;

      private static final String PASSWORD__PATTERN =
              "((?=.** \\d)(?=.** [a-z])(?=.** [A-Z])(?=.** [@#$%]).{6,20})";

      public PasswordValidator(){
          pattern = Pattern.compile(PASSWORD__PATTERN);
      }

     /** **
       **  Validate password with regular expression
       **  @param password password for validation
       **  @return true valid password, false invalid password
       ** /      public boolean validate(final String password){

          matcher = pattern.matcher(password);
          return matcher.matches();

      }
}

2.一致するパスワード:

{空} 1。 mkyong1A @ 2。mkYOn12 $

3.一致しないパスワード:

{空} 1。あまりにも短い、最小6文字2. mkyong12 @、大文字が必要です3. mkyoNg12

、特殊記号 ”

“はここでは使用できません4. mkyonG $$、数字が必要です5. MKYONG12 $、小文字は必須

ユニットテスト – PasswordValidator

ユニットテストはhttp://testng.org/doc/index.html[TestNG]で行います。

PasswordValidatorTest.java

package com.mkyong.regex;

import org.testng.Assert;
import org.testng.annotations.** ;
 /** **
 **  Password validator Testing
 **  @author mkyong
 **
 ** /public class PasswordValidatorTest {

    private PasswordValidator passwordValidator;

    @BeforeClass
        public void initData(){
        passwordValidator = new PasswordValidator();
        }

    @DataProvider
    public Object[][]ValidPasswordProvider() {
        return new Object[][]{
           {new String[]{
               "[email protected]", "mkYOn12$",
           }}
            };
    }

    @DataProvider
    public Object[][]InvalidPasswordProvider() {
        return new Object[][]{
           {new String[]{
               "[email protected]","[email protected]","mkyoNg12** ",
                            "mkyonG$$","MKYONG12$"
           }}
           };
    }

    @Test(dataProvider = "ValidPasswordProvider")
    public void ValidPasswordTest(String[]password) {

       for(String temp : password){
        boolean valid = passwordValidator.validate(temp);
        System.out.println("Password is valid : " + temp + " , " + valid);
        Assert.assertEquals(true, valid);
       }

    }

    @Test(dataProvider = "InvalidPasswordProvider",
                 dependsOnMethods="ValidPasswordTest")
    public void InValidPasswordTest(String[]password) {

       for(String temp : password){
        boolean valid = passwordValidator.validate(temp);
        System.out.println("Password is valid : " + temp + " , " + valid);
        Assert.assertEquals(false, valid);
       }
    }
}

5.ユニットテスト – 結果

Password is valid : [email protected] , true
Password is valid : mkYOn12$ , true
Password is valid : [email protected] , false
Password is valid : [email protected] , false
Password is valid : mkyoNg12**  , false
Password is valid : mkyonG$$ , false
Password is valid : MKYONG12$ , false
PASSED: ValidPasswordTest([Ljava.lang.String;@1d4c61c)
PASSED: InValidPasswordTest([Ljava.lang.String;@116471f)

===============================================
    com.mkyong.regex.PasswordValidatorTest
    Tests run: 2, Failures: 0, Skips: 0
===============================================


===============================================
mkyong
Total tests run: 2, Failures: 0, Skips: 0
===============================================