2017-09-13 47 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

相關問題