使用正则表达式在Java中使用密码

时间:2017-10-04 14:00:12

标签: java regex passwords

我正在尝试使用正则表达式创建密码,我不明白为什么我的代码不起作用:

import java.util.Scanner;

class test {

 public static void main (String[] args) {

    Scanner scanner = new Scanner(System.in);

    System.out.println("Enter a password please.");
        String password = scanner.nextLine();

    int x;
    String redex = "(^[0-9]+$)";
    String redexx = "(^[A-Z]+$)";

    boolean hasSpace = false;
    boolean hasUpperCase = false;
        boolean hasLetter = false;
        boolean hasDigit = false; // we set four booleans to be able to bring up individual error messages.

    if (password.length() > 8){
        hasLetter = true;
    } else {
        System.out.println("Your password needs to be at least 8 characters long.");                        

        if (password.matches(redexx)) { // we check to see if the password has at least an upper case letter, one or more digits and a space using regular expressions
            hasUpperCase = true;

        } if (password.matches(redexx)) { 
            hasDigit = true; 

        } if (password.indexOf(' ') == 1) {
            hasSpace = true; }

    } if (hasLetter && hasDigit && hasUpperCase && !hasSpace) {
        System.out.println("Your password is strong."); 
    } if (!hasDigit) {      
                System.out.println("You need to put numbers in your password."); 
    } if (!hasUpperCase) {
        System.out.println("You need to use an upper case letter in your password."); 
    } if (hasSpace) {
                    System.out.println("You need to delete any spaces in your password."); 
        } // if we use if statements (and not any "else" or "else if", we get to show all the possible error messages.
    }
}

在更正“正则表达式”和“regexx”之后,在编译之后,当我输入一个完全适用的密码时,它仍然会显示密码需要大写并且它也需要一个数字

2 个答案:

答案 0 :(得分:1)

试试这个:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter a password please.");
        String password = scanner.nextLine();

        boolean validLength = password.length() >= 8;
        boolean hasLetter = password.matches(".*[a-zA-Z].*");
        boolean hasDigit = password.matches(".*\\d.*");
        boolean hasSpace = password.matches(".*\\s.*");

        if (!validLength)
            System.out.println("The password must be at least 8 characters long.");
        else if (!hasLetter)
            System.out.println("The password must contain at least one letter.");
        else if (!hasDigit)
            System.out.println("The password must contain at least one digit.");
        else if (hasSpace)
            System.out.println("The password must not contain spaces.");
        else
            System.out.println("Your password is strong.");
    }
}

除了问题评论中提到的错误之外,您还使用了错误的正则表达式。有关如何在Java中使用正则表达式的信息,请阅读Java regular expression constructs

此外,空格字符实际上使密码更强,所以我建议允许用户输入空格字符。在我的答案的演示代码中,虽然不允许用户输入空格字符,因为这是您的请求。

答案 1 :(得分:0)

问题可能是因为您没有将“正则表达式”放在引号中。他们是字符串。

请尝试:

String redex = "(^[0-9]+$)";
String redexx = "(^[A-Z]+$)";