2016-06-14 72 views
0

我有一個正則表達式,確保用戶名只有來自AZ或AZ或0-9字符和3-15個字符之間的字符。Android的正則表達式驗證返回false

我想什麼爲每一個獨立的正則表達式:

1)檢查3-15個字符(任何符號任何字母之間的;只有 的事項就是節數字符)

2)必須包含az或AZ或0-9或。 _(點和 下劃線)(無空間/空格)

3)從AZ或az(字符可以包含空格/空格)

這工作得很好:

private static final String PASSWORD_PATTERN = "^[a-zA-Z0-9_-]{3,15}$"; 
etlogin_username.addValidator(new RegexpValidator("Regex wrong", USERNAME_PATTERN)); 

以下返回false:

1)USERNAME_PATTERN1返回假

2)REALNAME_PATTERN爲 「DAVIC Schummer」

private static final String USERNAME_PATTERN1 = "^\\w{3,15}$"; 
private static final String USERNAME_PATTERN2 = "^[a-zA-Z0-9_-]{3,15}$"; 

private static final String REALNAME_PATTERN = "^[a-zA-Z]$"; 

etlogin_username.addValidator(new RegexpValidator("Must be between 3-15 characters", USERNAME_PATTERN1)); 
etlogin_username.addValidator(new RegexpValidator("Symbols not allowed", USERNAME_PATTERN2)); 

etlogin_realname.addValidator(new RegexpValidator("Only alphabets allowed", REALNAME_PATTERN)); 

注意返回false 「dummyu :)!」:我知道如何將用戶string.length減;但我想它一個正則表達式,我可以把它傳遞給Validator,這需要很多其他的東西

照顧

EDIT1:新增的正則表達式驗證:

public class RegexpValidator extends METValidator { 

    private Pattern pattern; 

    public RegexpValidator(@NonNull String errorMessage, @NonNull String regex) { 
    super(errorMessage); 
    pattern = Pattern.compile(regex); 
    } 

    public RegexpValidator(@NonNull String errorMessage, @NonNull Pattern pattern) { 
    super(errorMessage); 
    this.pattern = pattern; 
    } 

    @Override 
    public boolean isValid(@NonNull CharSequence text, boolean isEmpty) { 
    return pattern.matcher(text).matches(); 
    } 
} 
+2

是在'RegexpValidator'固定的模式?如果不是,請嘗試3)中的「[a-zA-Z0-9 ._]」和2)和'「[a-zA-Z \ s]」。另外,爲了檢查長度而不考慮字符類型,我想你最好使用'「^。{3,15} $」'爲1)。 –

+0

REALNAME的驗證器爲「david schummer」和「davidschummer」返回false; 1&3正常工作 – Zen

+0

這聽起來並不一致:[第二個正則表達式應該在'david'和'david moretexthere'中找到部分匹配](https://regex101.com/r/fQ2aC5/1) –

回答

0

試試這個

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(); 

      } 
    } 
0

正則表達式中使用:

private static final String PASSWORD_PATTERN = "^.{3,16}$"; 

private static final String USERNAME_PATTERN1 = "^.{3,16}$"; 
private static final String USERNAME_PATTERN2 = "^[a-zA-Z0-9._-]$"; 

private static final String REALNAME_PATTERN1 = "^.{3,36}$"; 
private static final String REALNAME_PATTERN2 = "^[\\p{L} .'-]+$"; 
+0

正則表達式REALNAME_PATTERN2的解釋可以找到在這裏:http://stackoverflow.com/questions/15805555/java-regex-to-validate-full-name-allow-only-spaces-and-letters – Zen