Amc_rtty
Amc_rtty

Reputation: 3813

parse string with regex

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,

Upvotes: 2

Views: 376

Answers (5)

Adibe7
Adibe7

Reputation: 3539

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!

Upvotes: 2

Anton
Anton

Reputation: 5623

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.

Upvotes: 1

Merlyn Morgan-Graham
Merlyn Morgan-Graham

Reputation: 59111

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.

Upvotes: 0

David
David

Reputation: 73564

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

Upvotes: 5

Marc Gravell
Marc Gravell

Reputation: 1062780

^ 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.

Upvotes: 5

Related Questions