电子邮件的正则表达式,中间应包含连字符,但起始和结束位置不应包含连字符

时间:2019-04-03 15:55:24

标签: java regex

我有一个满足条件的正则表达式,但开头允许使用连字符。一开始如何限制。我想在开头使用连字符限制的正则表达式,因为它允许我需要一些其他字符。

/^[A-Z0-9-._%+]+@([A-Z0-9-]+\.)+[A-Z]{2,4}$/i;

预先感谢

1 个答案:

答案 0 :(得分:0)

这似乎对我有用

public class QuickyTest{

   static String[] testVectors = { "alaskjf", 
                          "o-kay@example.com",
                                 "-dash@x.AA", 
                                 "dash-@y.ZZ" };

   public static void main( String[] args ) {
      Pattern pattern = Pattern.compile( "(?i:[A-Z0-9._%+]+|[A-Z0-9._%+]+[A-Z0-9._%+-]+[A-Z0-9._%+]+@([A-Z0-9-]+.)+[A-Z]{2,4})" );
      for( String test : testVectors ) {
         Matcher match = pattern.matcher( test );
         System.out.println( test + " : " + match.matches() );
      }
   }

}