最好定期防爆pression在C#电子邮件验证 [英] Best Regular Expression for Email Validation in C#

查看:141
本文介绍了最好定期防爆pression在C#电子邮件验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所看到的普通的前pressions为不同的编程语言,所有声称验证电子邮件地址,众说纷纭。我看到很多评论说,有问题的前pressions不某些情况下工作,他们是不是太严或太宽松。我正在寻找的是一个普通的前pression我可以在我的C#code使用的是明确的。

我发现,最好的事情是这样

  ^([\\ W \\ \\  - ] +)@([\\ W \\  - ] +)(。(\\(\\ W){2,3})+) $

时有更好的东西?


解决方案

  

邮件地址:RFC 2822格式结果
  匹配一个正常的电子邮件地址。
  不检查的顶级域名。结果需要不区分大小写
  选项​​为ON。


<$p$p><$c$c>[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?

用法:

 布尔ISEMAIL = Regex.IsMatch(都是EmailString, @\"\\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\\Z\", RegexOptions.IgnoreCase);

I have seen a multitude of regular expressions for different programming languages that all purport to validate email addresses. I have seen many comments saying that the expressions in question do not work for certain cases and that they are either too strict or too permissive. What I'm looking for is a regular expression that I can use in my C# code that is definitive.

The best thing I have found is this

^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$

Is there something better?

解决方案

Email address: RFC 2822 Format
Matches a normal email address. Does not check the top-level domain.
Requires the "case insensitive" option to be ON.

[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?

Usage :

bool isEmail = Regex.IsMatch(emailString, @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z", RegexOptions.IgnoreCase);

这篇关于最好定期防爆pression在C#电子邮件验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆