为什么我的正则表达式无法匹配电子邮件?

时间:2021-01-24 15:39:08

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

但是b1和b2都失败了

1 个解决方案

#1


3  

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.

但是,话虽如此,您的正则表达式不是匹配电子邮件地址的好模式。事实上,准确的模式确实很难实现。谷歌一些,你应该找到更好的。

#1


3  

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.

但是,话虽如此,您的正则表达式不是匹配电子邮件地址的好模式。事实上,准确的模式确实很难实现。谷歌一些,你应该找到更好的。