2011-06-21 65 views
4

我想查看一個字符串是否至少包含一個數字或小寫或大寫字母。字符串包含至少一個數字

我寫了這樣的事情:

 int combinations = 0; 
     string pass = "!!!AAabas1"; 

     if (pass.matches("[0-9]")) { 
      combinations = combinations + 10; 
     } 

     if (pass.matches("[a-z]")) { 
      combinations =combinations + 26; 
     } 

     if (pass.matches("[A-Z]")) { 
      combinations =combinations + 26; 
     } 

但是我不明白爲什麼我不能讓組合去36,他們只停留在0。我在做什麼錯?

+0

給國際用戶的一些愛 - 爲了勝利UTF8!此外,使用標點符號和其他特殊字符的人也應該獲得額外的功勞。 –

+0

@Seth Robertson:我認爲Java的內部表示是UTF-16 ... – phooji

+0

@phooji:是的,但他只是給0-9A-Za-z的功勞,不包括空格/: - @ [-' { - 然後是完整的UTF-8字符集。 –

回答

10

您可以使用Pattern來代替,我認爲「matches」方法會查找整個字符串以匹配正則表達式。

試試下面的代碼:

int combinations = 0; 
    String pass = "!!AAabas1"; 
    if (Pattern.compile("[0-9]").matcher(pass).find()) { 
     combinations = combinations + 10; 
    } 

    if (Pattern.compile("[a-z]").matcher(pass).find()) { 
     combinations = combinations + 26; 
    } 

    if (Pattern.compile("[A-Z]").matcher(pass).find()) { 
     combinations = combinations + 26; 
    } 
+0

謝謝Hrzio的代碼示例。這真的很有幫助。解決了它。 – idipous

2

問題是matches試圖匹配整個輸入字符串。

而是嘗試創建一個Pattern,然後從那裏創建一個Matcher,然後使用find方法。

Pattern javadoc應該有很大的幫助。

1

在使用正則表達式這可以明顯的工作,GuavaCharMatcher類可能是有點更適合你想要做什麼:

if (CharMatcher.inRange('0', '9').matchesAnyOf(pass)) 
    combinations += 10; 
if (CharMatcher.inRange('a', 'z').matchesAnyOf(pass)) 
    combinations += 26; 
if (CharMatcher.inRange('A', 'Z').matchesAnyOf(pass)) 
    combinations += 26; 
4

這是我的嘗試。請注意,這使用unicode類別進行驗證,因此非拉丁語言友好。

import java.util.regex.Pattern; 

public class PasswordValidator { 

    public static void main(String[] args) { 
     final PasswordValidator passwordValidator = new PasswordValidator(); 
     for (String password : new String[] { "abc", "abc123", "ABC123", "abc123ABC", "!!!AAabas1", "гшщз", 
       "гшщзЧСМИ22" }) { 
      System.out.printf("Password '%s' is %s%n", password, passwordValidator.isValidPassword(password) ? "ok" 
        : "INVALID"); 
     } 
    } 
    private static final Pattern LOWER_CASE = Pattern.compile("\\p{Lu}"); 
    private static final Pattern UPPER_CASE = Pattern.compile("\\p{Ll}"); 
    private static final Pattern DECIMAL_DIGIT = Pattern.compile("\\p{Nd}"); 

    /** 
    * Determine if a password is valid. 
    * 
    * <p> 
    * A password is considered valid if it contains: 
    * <ul> 
    * <li>At least one lower-case letter</li> 
    * <li>At least one upper-case letter</li> 
    * <li>At least one digit</li> 
    * </p> 
    * 
    * @param password 
    *   password to validate 
    * @return True if the password is considered valid, otherwise false 
    */ 
    public boolean isValidPassword(final String password) { 
     return containsDigit(password) && containsLowerCase(password) && containsUpperCase(password); 
    } 

    private boolean containsDigit(final String str) { 
     return DECIMAL_DIGIT.matcher(str).find(); 
    } 

    private boolean containsUpperCase(final String str) { 
     return UPPER_CASE.matcher(str).find(); 
    } 

    private boolean containsLowerCase(final String str) { 
     return LOWER_CASE.matcher(str).find(); 
    } 

} 

下面是輸出:

Password 'abc' is INVALID 
Password 'abc123' is INVALID 
Password 'ABC123' is INVALID 
Password 'abc123ABC' is ok 
Password '!!!AAabas1' is ok 
Password 'гшщз' is INVALID 
Password 'гшщзЧСМИ22' is ok 
+0

謝謝。尼斯觸摸unicode輸入。可能實際證明非常有用! – idipous

1
String str_rno = "CooL8"; 

boolean Flag = false; 

String[] parts = str_rno.split(""); 

for (int i = 0; i < str_rno.length(); i++) { 

    String part1 = parts[i + 1];  

    if (Character.isDigit(str_rno.charAt(i))) { 
     System.out.println(" " + i + " " + part1 + " digit"); 
     Flag = true; 
    } else { 
     System.out.println(" " + i + " " + part1 + " char "); 
    } 

} 


if(Flag==true){ 
    Toast.makeText(getApplicationContext(),"String contain 1 digit",Toast.LENGTH_SHORT).show(); 
} 

if(Flag==flase){ 
    Toast.makeText(getApplicationContext(),"String not contain 1 digit",Toast.LENGTH_SHORT).show(); 
} 
相關問題