C#正则表达式校验某个字符串是否是合格的email

news/2024/7/7 9:52:56

C#正则表达式校验某个字符串是否是合格的email

可以借助正则表达式校验某个字符串是否是合规的电子邮箱。对于邮箱的正则表达式有严格的模式,如:^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$
对应的C#实现如下:

using System;
using System.Text.RegularExpressions;

namespace EmailRegexValidateExample
{
    /// <summary>
    /// 如何确认字符串是有效的电子邮件格式
    /// https://learn.microsoft.com/zh-cn/dotnet/standard/base-types/how-to-verify-that-strings-are-in-valid-email-format
    /// </summary>
    internal class Program
    {
        public static string EMAIL_PATTERN = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$";
        /// <summary>
        /// 验证字符串email是否为有效的邮箱地址
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public static bool IsEmail(string email)
        {
            Regex regEmail = new Regex(EMAIL_PATTERN);
            Match match = regEmail.Match(email);
            return match.Success;
        }
        static void Main(string[] args)
        {
            string email1 = "alice@example.com";
            if (IsEmail(email1))
            {
                Console.WriteLine("email1 " + email1 + " is valid email address");
            } else {
                Console.WriteLine("email1 " + email1 + " is invalid email address");
            }

            string email2 = "alice..bob@example.com";
            if (IsEmail(email1))
            {
                Console.WriteLine("email2 " + email2 + " is valid email address");
            } else {
                Console.WriteLine("email2 " + email1 + " is invalid email address");
            }
        }
    }
}

执行结果如下图所示:
C#正则校验合法的email邮箱地址


http://www.niftyadmin.cn/n/4828070.html

相关文章

SQLException: Communications link failure

连接数据库时出现如下错误提示&#xff1a;SQLException: Communications link failureThe last packet sent successfully to the serverwas 0 milliseconds ago. The driver has not received any packets from theserver.附上部分代码段&#xff1a;publicstaticConnection …

dom04location对象,定时跳转及其它三个对象

location地址栏对象 window.location(地址&#xff0c;对应的是浏览器的地址栏&#xff09;。 location.href”网页地址/电子邮件等”&#xff1b; console.log(window.location.hash);//锚点&#xff0c;一般用于做单页面应用时比较常用。 console.log(window.location.ho…

暑期留校之iOS学习笔记之视图控制器(UI)

2015.8.18 <pre name"code" class"objc">//首先要导入头文件 #import "AppDelegate.h" #import "RootViewController.h"interface AppDelegate ()endimplementation AppDelegate- (BOOL)application:(UIApplication *)applicati…

dom04 移动盒子案例

移动盒子案例 鼠标经过长图往上走&#xff0c;鼠标经过长图往下走 <script>var timernull;var y0;//用变量记录top的初始值var picdocument.getElementById(pic);var goUpdocument.getElementById(goUp);var goDowndocument.getElementById(goDown);goUp.onmouseoverfu…

白驹过隙之大一总结

时间过得可真快&#xff0c;刚刚进入大学一幕幕还没退却&#xff0c;我已经度过了一年的大学生活了简单的谈谈我的大一都经历了哪些“小事”。 刚进入大学就是各种的 军训&#xff0c;教官很好&#xff0c;经常让我们休息&#xff0c;齐步走&#xff0c;踢正步&#xff0c;站…

包装类、object、单例模式、final、抽象类

/*包装类*/ /* byte Byte short Short int Integer long Long char Character float Float double Double boolean Boolean 基本数据和其包装类:作为成员变量时的默认初始值的差异: int -- Integer 0 null */ class WrapDemo { public s…

白驹过隙之大一总结第二话

话说母教义工吧&#xff0c;参加这个社团&#xff0c;完全因为初衷想报答社会&#xff0c;为社会增加正能量&#xff0c;一听是义工再加上是同班同学推荐拉进的&#xff0c;一想可能不错有好有个伴&#xff0c;后来发现&#xff0c;原来母教才是重点&#xff0c;第一次开会就懂…

dom04 日期对象常用方法

日期对象&#xff1a; //通过构造函数创建日期对象 var datenew Date();//创建日期对象&#xff0c;数据类型为对象 console.log(date.toString());//可以把日期对象转换成字符串的形式。 console.log(date.valueOf());//可以把日期对象转换成数值的形式 13位的数字。 //1…