0

I have been using this regex to validate email addresses. Files found not to have valid Email addresses on a certain line are deleted:

 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();

                }

This piece of code has been working fine until one file appeared with an email Id :

textsam.textsample@somedomain.co.uk

The file was deleted as one that does not have a valid email address. Can someone please help me on how to include the above email address as a valid one?

Stanley Mungai
  • 4,044
  • 30
  • 100
  • 168
  • http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address – ellak Feb 19 '13 at 11:17

2 Answers2

3

Why are you limiting the email address to 32 characters ? The above is 34 characters, but you limit it via

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

See also this SO question and the answers and this web page discussing email address regexps (it's considerably more complicated than what you're doing currently, and I would rethink your approach re. using regexps)

Community
  • 1
  • 1
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
1

I believe you this is an error occurred due to limitation of characters. Always leave atleast 50 characters for an email address. My personal practice is 100, Also consider using Regular expressions inbuilt on the Microsoft visual studio, it should make things much easier for you.

Here's a link

http://msdn.microsoft.com/en-gb/library/system.text.regularexpressions.regex.aspx

Suits999
  • 369
  • 1
  • 7
  • 25