简体   繁体   中英

Why does my regex fail to match email?

I have to following code to validate an email address

 var reg = new Regex(@"/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/");

 string e1 = "name@host.net";
 string e2 = "namehost.net";

 bool b1 = reg.IsMatch(e1);
 bool b2 = reg.IsMatch(e2);

but both b1 and b2 fail

Remove the slashes at the beginning and end.

var reg = new Regex(@"^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$");

However, that being said, your regex is not a good pattern for matching e-mail addresses. In fact, an accurate pattern is really difficult to make. Google some and you should find better ones.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM