2015-03-03 25 views

回答

0

以及不知道验证多少还是让这里的意义,但是这将工作...

^+ @ - ((?!):[A-ZA-Z \ d - ] { ){1,126}(?!\ d +)[a-zA-Z \ d] {1,63} $

你明显可以改变第一个。做一组更严格的字符的...

1

您可以使用此模式(已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])+)\\])" 
0

公共类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); 
      } 
     } 
} 

}

相关问题