我尝试用在代码中包含具有相同模式的密码来替换用户输入,并且没有出现问题。
另一个问题:Java正则表达式中空白的符号是什么?
注意:我是新来Java的,所以我的代码可能看起来有点混乱
import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public static void main(String[] args) {
String email_pattern = "\\w{7,20}@(gmail|Hotmail|yahoo)\\.com"; // email pattern String password_pattern=".{10,20}"; //password pattern`(problem in this statement)
Scanner s = new Scanner(System.in);
String email;
String password;
System.out.println("Welcome to my site"); //
System.out.print("Enter your email: ");
email = s.next(); //email input by user
Pattern p = Pattern.compile(email_pattern);
Matcher m = p.matcher(email);
if (m.matches()) {
System.out.print("\nEnter your password: ");
password = s.next(); //password input by user
Pattern p2 = Pattern.compile(password_pattern);
Matcher m2 = p2.matcher(password);
if (m2.matches()) {
System.out.print("\n You are logged in");
} else {
System.out.print("\n" + m2.matches); // outputs the matching result if the password has a wrong format
}
} else {
System.out.print("\nWrong email format please re-enter your email");
}
}
//output problem in password matching with white spaces
只需为emailId和password设置regex即可。有许多方法可以为emailId和password设置regex。 这里有一个简单的例子和描述。
电子邮件的正则表达式=^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$;
描述
^ # start of the line
[_A-Za-z0-9-\\+]+ # must start with string in the bracket [ ], must contains one or more (+)
( # start of group #1
\\.[_A-Za-z0-9-]+ # follow by a dot "." and string in the bracket [ ], must contains one or more (+)
)* # end of group #1, this group is optional (*)
@ # must contains a "@" symbol
[A-Za-z0-9-]+ # follow by string in the bracket [ ], must contains one or more (+)
( # start of group #2 - first level TLD checking
\\.[A-Za-z0-9]+ # follow by a dot "." and string in the bracket [ ], must contains one or more (+)
)* # end of group #2, this group is optional (*)
( # start of group #3 - second level TLD checking
\\.[A-Za-z]{2,} # follow by a dot "." and string in the bracket [ ], with minimum length of 2
) # end of group #3
$ # end of the line
密码的正则表达式=((?=.*\\d)(?=.*[a-z])(?=.*[a-z])(?=.*[@#$%])。{10,20});
描述
( # 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
{10,20} # length at least 10 characters and maximum of 20
) # End of group
完整的代码如下所示
public static void main(String[] args) {
String email_pattern = "^[A-Za-z0-9+_.-]+@(.+)$";
String password_pattern = "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{10,20})";
String email;
String password;
Scanner s=new Scanner(System.in);
System.out.println("Welcome to my site"); /
System.out.print("Enter your email: ");
email=s.next();
Pattern p=Pattern.compile(email_pattern);
Matcher m=p.matcher(email);
if(m.matches()){
System.out.print("\nEnter your password: ");
password=s.next();
Pattern p2=Pattern.compile(password_pattern);
Matcher m2=p2.matcher(password);
if(m2.matches()){
System.out.print("\n You are logged in");
}else{
System.out.print("\n"+m2.matches);
}
}else{
System.out.print("\nWrong email format please re-enter your email");
}
}
您的代码有一个问题,那就是Java函数,而不是正则表达式模式。请更正这个问题以解决您的问题:
将每个s.next()
替换为
s.nextLine()
因为s.next()方法返回next token
意指输入的单词,并以空格作为next token的分隔符。
查看scanner.next()的实现:https://www.tutorialspoint.com/java/util/scanner_next.htm