开发者

Email Validation: converting a regular expression written in PHP (preg) to .NET (Regex)

Based on this answer... Using a regular expression to validate an email address

Which led me to th开发者_JS百科is site... http://fightingforalostcause.net/misc/2006/compare-email-regex.php

I'd like to use this regex for email validation for my ASP.NET MVC app:

/^[-_a-z0-9\'+*$^&%=~!?{}]++(?:\.[-_a-z0-9\'+*$^&%=~!?{}]+)*+@(?:(?![-.])[-a-z0-9.]+(?<![-.])\.[a-z]{2,6}|\d{1,3}(?:\.\d{1,3}){3})(?::\d++)?$/iD

Unfortunately, I get this error

System.ArgumentException was unhandled by user code Message="parsing \"/^[-_a-z0-9\'+$^&%=~!?{}]++(?:\.[-_a-z0-9\'+$^&%=~!?{}]+)*+@(?:(?![-.])[-a-z0-9.]+(?

Has anyone ever converted this to be usable by .NET's Regex class, or is there another .NET regular expression class that is a better fit with PHP's preg_match function?


The problem with your regular expression in .NET is that the possessive quantifiers aren't supported. If you remove those, it works. Here's the regular expression as a C# string:

@"^[-_a-z0-9\'+*$^&%=~!?{}]+(?:\.[-_a-z0-9\'+*$^&%=~!?{}]+)*@(?:(?![-.])[-a-z0-9.]+(?<![-.])\.[a-z]{2,6}|\d{1,3}(?:\.\d{1,3}){3})(?::\d+)?$"

Here's a test bed for it based on the page you linked to, including all the strings that should match and the first three of those that shouldn't:

using System;
using System.Text.RegularExpressions;

public class Program
{
    static void Main(string[] args)
    {
        foreach (string email in new string[]{
            "l3tt3rsAndNumb3rs@domain.com",
            "has-dash@domain.com",
            "hasApostrophe.o'leary@domain.org",
            "uncommonTLD@domain.museum",
            "uncommonTLD@domain.travel",
            "uncommonTLD@domain.mobi",
            "countryCodeTLD@domain.uk",
            "countryCodeTLD@domain.rw",
            "lettersInDomain@911.com",
            "underscore_inLocal@domain.net",
            "IPInsteadOfDomain@127.0.0.1",
            "IPAndPort@127.0.0.1:25",
            "subdomain@sub.domain.com",
            "local@dash-inDomain.com",
            "dot.inLocal@foo.com",
            "a@singleLetterLocal.org",
            "singleLetterDomain@x.org",
            "&*=?^+{}'~@validCharsInLocal.net",
            "missingDomain@.com",
            "@missingLocal.org",
            "missingatSign.net"
        })
        {
            string s = @"^[-_a-z0-9\'+*$^&%=~!?{}]+(?:\.[-_a-z0-9\'+*$^&%=~!?{}]+)*@(?:(?![-.])[-a-z0-9.]+(?<![-.])\.[a-z]{2,6}|\d{1,3}(?:\.\d{1,3}){3})(?::\d+)?$";
            bool isMatch = Regex.IsMatch(email, s, RegexOptions.IgnoreCase);
            Console.WriteLine(isMatch);
        }
    }
}

Output:

True
True
True
True
True
True
True
True
True
True
True
True
True
True
True
True
True
True
False
False
False

A problem though is that it fails to match some valid email-addresses, such as foo\@bar@example.com. It's better too match too much than too little.


You really shouldn't be using a RegEx to parse email addresses in .NET. Your better option is to use the functionality built into the framework.

Try to use your email string in the constructor of the MailAddress class. If it throws a FormatException then the address is no good.

try 
{
    MailAddress addr = new MailAddress("theEmail@stackoverflow.com")
    // <- Valid email if this line is reached
}
catch (FormatException)
{
    // <- Invalid email if this line is reached
}

You can see an answer a Microsoft developer gave to another email validation question, where he explains how .NET's email parsing has also improved dramatically in .NET 4.0. Since at the time of answering this, .NET 4.0 is still in beta, you probably aren't running it, however even previous versions of the framework have adequate email address parsing code. Remember, in the end you're most likely going to be using the MailAddress class to send your email anyway. Why not use it to validation your email addresses. In the end, being valid to the MailAddress class is all that matters anyway.


.NET regular expression syntax is not the same as in PHP, and Regex is the only built-in class to use regular expression (but there might be other third party implementation). Anyway, it's pretty easy to validate an email address with Regex... straight from the source

^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$


I've used this function before in a bunch of e-commerce applications and never had a problem.

    public static bool IsEmailValid(string emailAddress)
    {
        Regex emailRegEx = new Regex(@"\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b");
        if (emailRegEx.IsMatch(emailAddress))
        {
            return true;
        }

        return false;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜