ユーザー名正規表現パターン

^[a-z0-9__-]{3,15}$

説明

^                    # Start of the line
 [a-z0-9__-]        # Match characters and symbols in the list, a-z, 0-9, underscore, hyphen
             {3,15}  # Length at least 3 characters and maximum length of 15
$                    # End of the line

全体の組み合わせは、小文字、数字、特殊記号 “__-“のみを使用して、3〜15文字を意味します。これは、さまざまなウェブサイトで広く使用されている一般的なユーザー名パターンです。

1. Javaの正規表現の例

UsernameValidator.java

package com.mkyong.regex;

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

public class UsernameValidator{

      private Pattern pattern;
      private Matcher matcher;

      private static final String USERNAME__PATTERN = "^[a-z0-9__-]{3,15}$";

      public UsernameValidator(){
          pattern = Pattern.compile(USERNAME__PATTERN);
      }

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

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

      }
}

2.一致するユーザー名:

{空} 1。 mkyong34 2. mkyong2002 3. mkyong-2002 4. mk3-4__yong

3.一致しないユーザー名:

{空} 1。 mkong( “@”文字は許可されていません)3. mkyong123456789__-(長すぎる、最大15文字)

ユニットテスト – UsernameValidator

testNGを使用して単体テストを実行する。

UsernameValidatorTest.java

package com.mkyong.regex;

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

    private UsernameValidator usernameValidator;

    @BeforeClass
        public void initData(){
        usernameValidator = new UsernameValidator();
        }

    @DataProvider
    public Object[][]ValidUsernameProvider() {
        return new Object[][]{
           {new String[]{
                 "mkyong34", "mkyong__2002","mkyong-2002" ,"mk3-4__yong"
           }}
                };
    }

    @DataProvider
    public Object[][]InvalidUsernameProvider() {
        return new Object[][]{
           {new String[]{
             "mk","[email protected]","mkyong123456789__-"
           }}
            };
    }

    @Test(dataProvider = "ValidUsernameProvider")
    public void ValidUsernameTest(String[]Username) {

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

    }

    @Test(dataProvider = "InvalidUsernameProvider",
                 dependsOnMethods="ValidUsernameTest")
    public void InValidUsernameTest(String[]Username) {

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

    }
}

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

Username is valid : mkyong34 , true
Username is valid : mkyong__2002 , true
Username is valid : mkyong-2002 , true
Username is valid : mk3-4__yong , true
username is valid : mk , false
username is valid : [email protected] , false
username is valid : mkyong123456789__- , false
PASSED: ValidUsernameTest([Ljava.lang.String;@1d4c61c)
PASSED: InValidUsernameTest([Ljava.lang.String;@116471f)

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


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