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 :

[email protected]

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?

1

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.