在C#中,你可以使用Regex
类来匹配邮箱地址。一个简单的匹配邮箱的正则表达式是:^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
。这个正则表达式会匹配大多数常见的电子邮件地址格式。
以下是一个使用这个正则表达式匹配邮箱的C#代码示例:
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
string pattern = @"^[\w\.-]+@[\w\.-]+\.\w+$";
string input = "example@example.com";
Regex regex = new Regex(pattern);
bool match = regex.IsMatch(input);
if (match)
{
Console.WriteLine("邮箱地址有效");
}
else
{
Console.WriteLine("邮箱地址无效");
}
}
}
在这个示例中,pattern
变量包含了正则表达式,input
变量包含了要检查的字符串。Regex.IsMatch()
方法用于检查输入字符串是否与正则表达式匹配,返回一个布尔值。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
相关推荐:Ruby正则表达式如何匹配邮箱