1. Trang chủ
  2. » Công Nghệ Thông Tin

JavaCore tutorials

336 809 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 336
Dung lượng 2,95 MB

Nội dung

Java regular expression 1. How to validate username with regular expression Remember: Username Regular Expression Pattern ^[a-z0-9_-]{3,15}$ Description ^ # 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 Whole combination is means, 3 to 15 characters with any lower case character, digit or special symbol “_-” only. This is common username pattern that’s widely use in different websites. Example: UsernameValidator.java package org.quangthao.validateUser; import java.util.regex.Matcher; import java.util.regex.Pattern; public class UsernameValidator { private Pattern pattern; //Khuân mẫu private Matcher matcher; //Khớp(phù hợp) private static final String USERNAME_PATTERN="^[a-z0-9_-]{3,15}$"; public UsernameValidator(){ pattern=Pattern.compile(USERNAME_PATTERN); } public boolean validate(final String username){ matcher=pattern.matcher(username); return matcher.matches(); } } TestUsernameValidator.java package org.quangthao.validateUser; public class TestUsernameValidator { private UsernameValidator usernameValidator; public TestUsernameValidator(){ usernameValidator=new UsernameValidator(); } public Object[] ValidUsernameProvider(){ return new Object[]{"mkyong34", "mkyong_2002","mkyong- 2002" ,"mk3-4_yong"}; } public Object[] InvalidUsernameProvider(){ return new Object[]{"mk","mk@yong","mkyong123456789_-"}; } public UsernameValidator getUsernameValidator() { return usernameValidator; } public void setUsernameValidator(UsernameValidator usernameValidator) { this.usernameValidator = usernameValidator; } public static void main(String[] args){ TestUsernameValidator test=new TestUsernameValidator(); boolean valid; Object objectValid[]=test.ValidUsernameProvider(); Object objectInvalid[]=test.InvalidUsernameProvider(); for(int i=0;i<objectValid.length;i++){ valid=test.getUsernameValidator().validate(String.valueOf(objectValid[i])); System.out.println(objectValid[i]+":"+valid); } for(int i=0;i<objectInvalid.length;i++){ valid=test.getUsernameValidator().validate(String.valueOf(objectInvalid[i])); System.out.println(objectInvalid[i]+":"+valid); } } } Result: mkyong34:true mkyong_2002:true mkyong-2002:true mk3-4_yong:true mk:false mk@yong:false mkyong123456789_-:false Reference: How to validate username with regular expression Posted on November 6, 2009 , Last modified : October 11, 2012 By mkyong Username Regular Expression Pattern ^[a-z0-9_-]{3,15}$ Description ^ # 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 Whole combination is means, 3 to 15 characters with any lower case character, digit or special symbol “_-” only. This is common username pattern that’s widely use in different websites. 1. Java Regular Expression Example 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. Username that match: 1. mkyong34 2. mkyong_2002 3. mkyong-2002 4. mk3-4_yong 3. Username that doesn’t match: 1. mk (too short, min 3 characters) 2. mk@yong (“@” character is not allow) 3. mkyong123456789_- (too long, max characters of 15) 4. Unit Test – UsernameValidator Using testNG to perform unit test. 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","mk@yong","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. Unit Test – Result 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 : mk@yong , false username is valid : mkyong123456789_- , false PASSED: ValidUsernameTest([Ljava.lang.String;@1d4c61c) PASSED: InValidUsernameTest([Ljava.lang.String;@116471f) =============================================== com.mkyong.regexusernameValidatorTest Tests run: 2, Failures: 0, Skips: 0 =============================================== =============================================== mkyong Total tests run: 2, Failures: 0, Skips: 0 =============================================== 2. How to validate password with regular expression Remember: Password Regular Expression Pattern ((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20}) Description ( # 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 ?= – means apply the assertion condition, meaningless by itself, always work with other combination Whole combination is means, 6 to 20 characters string with at least one digit, one upper case letter, one lower case letter and one special symbol (“@#$%”). This regular expression pattern is very useful to implement a strong and complex password. Example: PasswordValidator.java package org.quangthao.passwordvalidate; import java.util.regex.Matcher; import java.util.regex.Pattern; public class PasswordValidator { private Pattern pattern; private Matcher matcher; private static final String USERNAME_PATTERN="((?=.*\\d)(?=.*[a-z])(? =.*[A-Z])(?=.*[@#$%]).{6,20})"; public PasswordValidator(){ pattern=Pattern.compile(USERNAME_PATTERN); } public boolean validate(final String username){ matcher=pattern.matcher(username); return matcher.matches(); } } TestPasswordValidator.java package org.quangthao.passwordvalidate; import org.quangthao.validateUser.TestUsernameValidator; public class TestPasswordValidator { private PasswordValidator passwordvalidator; public TestPasswordValidator(){ passwordvalidator=new PasswordValidator(); } public Object[] ValidPasswordProvider(){ return new Object[]{"mkyong1A@", "mkYOn12$"}; } public Object[] InvalidPasswordProvider(){ return new Object[]{"mY1A@","mkyong12@","mkyoNg12*","mkyonG$ $","MKYONG12$"}; } public PasswordValidator getPasswordvalidator() { return passwordvalidator; } public void setPasswordvalidator(PasswordValidator passwordvalidator) { this.passwordvalidator = passwordvalidator; } public static void main(String[] args){ TestPasswordValidator test=new TestPasswordValidator(); boolean valid; Object objectValid[]=test.ValidPasswordProvider(); Object objectInvalid[]=test.InvalidPasswordProvider(); for(int i=0;i<objectValid.length;i++){ valid=test.getPasswordvalidator().validate(String.valueOf(objectValid[i])); System.out.println(objectValid[i]+":"+valid); } for(int i=0;i<objectInvalid.length;i++){ valid=test.getPasswordvalidator().validate(String.valueOf(objectInvalid[i])); System.out.println(objectInvalid[i]+":"+valid); } } } Result: mkyong1A@:true mkYOn12$:true mY1A@:false mkyong12@:false mkyoNg12*:false mkyonG$$:false MKYONG12$:false Reference: How to validate password with regular expression Posted on November 6, 2009 , Last modified : October 11, 2012 By mkyong Password Regular Expression Pattern ((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20}) Description ( # 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 ?= – means apply the assertion condition, meaningless by itself, always work with other combination Whole combination is means, 6 to 20 characters string with at least one digit, one upper case letter, one lower case letter and one special symbol (“@#$%”). This regular expression pattern is very useful to implement a strong and complex password. P.S The grouping formula order is doesn’t matter. 1. Java Regular Expression Example 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. Password that match: 1. mkyong1A@ 2. mkYOn12$ 3. Password that doesn’t match: 1. mY1A@ , too short, minimum 6 characters 2. mkyong12@ , uppercase characters is required 3. mkyoNg12* , special symbol “*” is not allow here 4. mkyonG$$, digit is required 5. MKYONG12$ , lower case character is required 4. Unit Test – PasswordValidator Unit test with 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[] { "mkyong1A@", "mkYOn12$", }} }; } @DataProvider public Object[][] InvalidPasswordProvider() { return new Object[][]{

Ngày đăng: 06/05/2014, 13:54

Xem thêm

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w