寻找正则表达式通过下面的字符串作为一个有效的电子邮件:寻找一个正则表达式来通过这个电子邮件作为有效
String email = ""[email protected]"@example.com";
是邮件开头“只是在@符号之前,还有另一种”。由于
寻找正则表达式通过下面的字符串作为一个有效的电子邮件:寻找一个正则表达式来通过这个电子邮件作为有效
String email = ""[email protected]"@example.com";
是邮件开头“只是在@符号之前,还有另一种”。由于
以及不知道验证多少还是让这里的意义,但是这将工作...
^+ @ - ((?!):[A-ZA-Z \ d - ] { ){1,126}(?!\ d +)[a-zA-Z \ d] {1,63} $
你明显可以改变第一个。做一组更严格的字符的...
您可以使用此模式(已Java的转义字符串):
"(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])"
公共类Regex_mail {
public enum Mail {
gmail ("gmail"),
yahoo ("yahoo"),
facebook ("facebook"),
outlook ("outlook");
Mail(String g) {
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args){
Mail[] m = Mail.values();
String formatedString = Arrays.toString(m)
.replace(",", "|") //remove the commas
.replace(" ", "") //remove the right bracket
.replace("[", "(")
.replace("]", ")")
.replace("", "")//remove the left bracket
.trim();
Scanner in = new Scanner(System.in);
while (true) {
Pattern pattern = Pattern.compile("[a-zA-Z0-9]"+"@"+formatedString+".com");
Matcher matcher =
pattern.matcher(in.next());
boolean found = false;
while (matcher.find()) {
System.out.printf("mail accepted\n",
matcher.group(),
matcher.start(),
matcher.end());
found = true;
}
if(!found){
System.err.println("not valid e-mail address.\n");
//System.exit(0);
}
}
}
}
这将如果你逃脱内部的'''字符帮助。 – Dannnno 2015-03-03 02:14:09