Based on this answer…
Using a regular expression to validate an email address
Which led me to this 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:
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:
Output:
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.