Archive

Archive for the ‘Regex’ Category

Regex Validator for Email

June 7, 2009 Leave a comment

Original article here:
http://www.cambiaresearch.com/c4/bf974b23-484b-41c3-b331-0bd8121d5177/Parsing-Email-Addresses-with-Regular-Expressions.aspx

public bool TestEmailRegex(string emailAddress)
{
//                string patternLenient = @"w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*";
//                Regex reLenient = new Regex(patternLenient);
    string patternStrict = @"^(([^<>()[]\.,;: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 reStrict = new Regex(patternStrict);
//                      bool isLenientMatch = reLenient.IsMatch(emailAddress);
//                      return isLenientMatch;
    bool isStrictMatch = reStrict.IsMatch(emailAddress);
    return isStrictMatch;
}

Advertisements
Categories: Regex
%d bloggers like this: