2017-09-13 49 views
1

这是我先前的职位的延续,我的代码:正则表达式电子邮件验证

public class Main { 

static String theFile = "C:\\Users\\Pc\\Desktop\\textfile.txt"; 

public static boolean validate(String input) { 
    boolean status = false; 
    String REGEX = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" 
      + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; 

    Pattern pattern = Pattern.compile(REGEX); 
    Matcher matcher = pattern.matcher(input); 
    if (matcher.matches()) { 
     status = true; 
    } else { 
     status = false; 
    } 
    return status; 
} 

public static void main(String[] args) { 
    BufferedReader br = null; 

    try { 
     br = new BufferedReader(new FileReader(theFile)); 
     String line; 
     int count = 0; 
     while ((line = br.readLine()) != null) { 
      String[] arr = line.split("#"); 
      for (int x = 0; x < arr.length; x++) { 
       if (arr[x].equals(validate(theFile))) { 
        count++; 
       } 
       System.out.println("no of matches " + count); 

      } 
     } 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    Main.validate(theFile); 
} 

} 

它显示的结果: 没有比赛0 的不匹配0 没有比赛0 的不匹配0

,这是我在输入文件中的文本 [email protected][email protected][email protected] #[email protected]

我的输出应该是三只邮件,因为... E中的最后一个字符串是不是一个标准的电子邮件格式 我知道我不是想通过(arr[x].validate(theFile)))

+0

写一封电子邮件验证之前,请看看这些[有效和无效的电子邮件地址的实例(https://en.wikipedia.org/wiki/Email_address#Examples) – Andreas

+0

的http:// emailregex .com –

回答

0

我一直用这样的:

public static bool Validate(string email) 
    { 
     string expressions = @"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"; 
     return Regex.IsMatch(email, expressions); 
    } 

注:我也有一个功能,“清洁”的字符串应该有多个@符号也。

编辑:这里是我如何清理额外的@符号。请注意,这将保持它发现的第一个@,并删除其余部分。在您运行验证功能之前,应使用此功能。

public static string CleanEmail(string input) 
    { 
     string output = ""; 

     try 
     { 
      if (input.Length > 0) 
      { 
       string first_pass = Regex.Replace(input, @"[^\w\[email protected]]", ""); 
       List<string> second_pass = new List<string>(); 
       string third_pass = first_pass; 
       string final_pass = ""; 

       if (first_pass.Contains("@")) 
       { 
        second_pass = first_pass.Split('@').ToList(); 

        if (second_pass.Count >= 2) 
        { 
         string second_pass_0 = second_pass[0]; 
         string second_pass_1 = second_pass[1]; 

         third_pass = second_pass_0 + "@" + second_pass_1; 

         second_pass.Remove(second_pass_0); 
         second_pass.Remove(second_pass_1); 
        } 
       } 

       if (second_pass.Count > 0) 
       { 
        final_pass = third_pass + string.Join("", second_pass.ToArray()); 
       } 
       else 
       { 
        final_pass = third_pass; 
       } 

       output = final_pass; 

      } 
     } 
     catch (Exception Ex) 
     { 

     } 

     return output; 
    } 
+0

有什么功能?有趣 – valentine

+0

@valentine PM me。 –

+0

怎么样?我是新人@大卫本特利 – valentine

0

有几个错误在你的代码:

  1. if (arr[x].equals(validate(theFile)))检查邮件地址字符串是否等于布尔值你可以从validate()方法中获得。这绝不会是这样。
  2. validate()方法中,如果您只想检查字符串是否与正则表达式匹配,那么您可以简单地使用string.matches(pattern)来做到这一点 - 因此您只需要一行代码(不是真的有错误,但是这样更优雅)
  3. 将输入字符串(行)分割后,会留下空格,因为您只分割#符号。您可以trim()每串事后删除那些(见下面的代码)或split()\\s*#\\s*,而不是仅仅#

这里是所有的修复为例(我离开了,你读文件和部分使用字符串的邮件地址,而不是!):

public class Main { 

    private static final String PATTERN_MAIL 
     = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; 

    public static boolean validate(String input) { 
     return input.matches(PATTERN_MAIL); 
    } 

    public static void main(String[] args) { 
     String line = "[email protected] # [email protected] # [email protected] #[email protected]"; 
     String[] arr = line.split("#"); 
     int count = 0; 

     for (int x = 0; x < arr.length; x++) { 
      if (validate(arr[x].trim())) { 
       count++; 
      } 
      System.out.println("no of matches " + count); 
     } 
    } 

} 

它打印:

no of matches 1 
no of matches 2 
no of matches 3 
no of matches 4 

编辑:如果模式不应该要匹配最后一个邮件地址,您必须更改该模式。现在它匹配所有这些。

+0

感谢您更正指出 – valentine

+0

如果我的答案解决了您的问题,您可以将其标记为已被他人接受并用于我的毫无价值的声望点:) – mumpitz

相关问题