tags:

views:

52

answers:

5

Hello everyone,

I am trying to search a string for email addresses, but my regex does not work, when the string contains other characters than the email. Meaning, if I try on a small string like "[email protected]", the regex finds a match. If I insert a blank space in the string, like: " [email protected]", the regex does not find an email match.

Here is my code(the regex pattern is from the web):

            string emailpattern = @"^(([^<>()[\]\\.,;:\s@\""]+"

                    + @"(\.[^<>()[\]\\.,;:\s@\""]+)*)|(\"".+\""))@"

                    + @"((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"

                    + @"\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+"

                    + @"[a-zA-Z]{2,}))$";
            Regex rEmail = new Regex(emailpattern);
            string str = @" [email protected]";
            MatchCollection mcolResults = rEmail.Matches(str);
            MessageBox.Show(mcolResults.Count.ToString());

Please let me know what am I doing wrong.

Thank you.

Best regards,

+5  A: 

^ and $ mean (respectively) the start and end of the input text (or line in multi-line mode) - generally used to check that the entire text (or line) matches the pattern. So if you don't want that, take them away.

Marc Gravell
Man you're quick. +1.
David Stratton
Thank you very much for your help, I see now where the mistake was. I will try your suggestion when I get home. Next time I'll learn to make a regex before asking!
Andrei
+4  A: 

Remove the ^ and the $ from the beginning and the end. They mean "Start of string" and "End of string" respectively.

David Stratton
A: 

The regex is correct. e-mail addresses don't contain whitespace.

You can use escapes like \w in your regex in order to match whitespace, or you can do str.Trim() to fix your string before trying to match against it.

Merlyn Morgan-Graham
Thank you for your suggestion; however I cannot use trim() - the strings are dynamic, and if for example it comes as "mmm [email protected]", if i remove the whitespace the regex will match: [email protected]
Andrei
@Andrei: Trim does not remove all whitespace, only whitespace at the beginning and end of the string.
Merlyn Morgan-Graham
+1  A: 

First obvious problem: Your expression only matches email adresses at the start of a string. You need to drop the ^ at the start.

^ matches the start of a string.

Anton
+2  A: 

Do you learn how to use regex or you actually need to parse email addresses?

There is an object that was especially designed to do it MailAddress

Here is the MSDN documentation: http://msdn.microsoft.com/en-us/library/591bk9e8.aspx

When you initialize it with a string that holds a mail address that is not in the correct format, a FormatException will be thrown.

Good luck!

Adibe7
@Adibe7: +1 Even if the OP doesn't use this answer, the MailAddress class is a great find for me. Thanks!
Merlyn Morgan-Graham