2013-02-19 183 views
0

我一直在使用這個正則表達式來驗證電子郵件地址。找到的文件不會對一定行有效的電子郵件地址被刪除:使用正則表達式驗證電子郵件地址

FileInputStream fsdel = new FileInputStream("C:/Folder/" + filefinal[o]); 
       BufferedReader brdel = new BufferedReader(new InputStreamReader(fsdel)); 
       for (int j = 0; j < 4; j++) { 
        brdel.readLine(); 
       } 
       String email = brdel.readLine(); 
       String mine = email.trim(); 
       String lineIwant = mine.substring(0, 32).trim(); 
       // System.out.println("EMAIL ID: " + lineIwant); 
       String emailreg = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; 
       Boolean b = lineIwant.matches(emailreg); 

       if (b.toString() == "false") { 
        System.out.println(filedel[o]); 
        fsdel.close(); 
        //brdel.close(); 
        filedel[o].delete(); 

       } 

這段代碼一直工作正常,直到一個文件出現了一個電子郵件ID:

[email protected] 

該文件已被刪除作爲一個沒有有效的電子郵件地址。有人可以幫助我如何包含上述電子郵件地址作爲一個有效的?

+0

http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address – ellak 2013-02-19 11:17:26

+0

'+ @。 + \\。[az] +' – 2013-02-19 11:20:35

回答

3

爲什麼要將電子郵件地址限制爲32個字符?以上是34個字符,但你通過

String lineIwant = mine.substring(0, 32).trim(); 

見限制也this SO question and the answersthis web page討論的電子郵件地址的正則表達式(這是大大比你目前正在做的事情比較複雜,我會重新考慮你的方法重新。使用正則表達式)

+0

對不起,我從來沒有注意到....我的錯。 – ErrorNotFoundException 2013-02-19 11:31:46

相關問題