using System.Text.RegularExpressions;
namespace Atomx.Utils.Extension
{
public class RegexExtension
{
///
/// 判断是否是电子邮件格式
///
///
///
public static bool IsEmail(string email)
{
return Regex.IsMatch(email, @"^\S+@\S+\.\S+$");
}
///
/// 判断手机号码是否符合规则,不考虑国家代码的情况下
///
///
///
public static bool IsValidMobile(string mobile)
{
string pattern = "^\\d{7,15}$";
return Regex.IsMatch(mobile, pattern);
}
///
/// 判断手机号码是否符合规则,不考虑国家代码的情况下
///
///
///
public static bool IsValidGlobalMobile(string mobile)
{
// 正则表达式规则解释:
// ^ 开始
// \\+ 匹配一个加号 (E.164要求号码以加号开始)
// \\d{1,3} 匹配1到3位数字,即国家代码
// \\d{7,15} 匹配7到15位数字,即电话号码
// $ 结束
string pattern = "^\\+\\d{1,3}\\d{7,15}$";
return Regex.IsMatch(mobile, pattern);
}
///
/// 验证账号是否是邮箱地址或者手机号
///
///
///
public static bool IsValidAccount(string account)
{
string pattern = @"^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$";
if (Regex.IsMatch(account, pattern))
{
return true;
}
return IsValidMobile(account);
}
}
}