/// <summary>
/// 邮箱格式验证
/// </summary>
/// <returns></returns>
public static string CheckMail(string strEmail)
{
string result = "";
Regex regex = new Regex(@"[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}");
Match match = regex.Match(strEmail);
if(match.Success)
{
result = strEmail;
}
else
{
result = "无效邮箱";
}
return result;
}
/// <summary>
/// 转换日期格式(yyyy-MM-dd 转 yyyy年MM月dd日)
/// </summary>
/// <param name="strDate"></param>
/// <returns></returns>
public static string TransDate(string strDate)
{
string result = "";
Regex regex = new Regex(@"(\d+)-(\d+)-(\d+)");
if(regex.IsMatch(strDate))
{
result = regex.Replace(strDate,"$1年$2月$3日");
}
return result;
}
/// <summary>
/// 正则表达式提取和替换内容
/// </summary>
public static string Contentextract()
{
string result = "";
string str = "大家好! <User EntryTime='2010-10-7' Email='zhangsan@163.com'>张三</User> 自我介绍。";
Regex regex = new Regex(@"<User\s*EntryTime='(?<time>[\s\S]*?)'\s+Email='(?<email>[\s\S]*?)'>(?<userName>[\s\S]*?)</User>", RegexOptions.IgnoreCase);
Match match = regex.Match(str);
if(match.Success)
{
string userName = match.Groups["userName"].Value; //获取用户名
string time = match.Groups["time"].Value; //获取入职时间
string email = match.Groups["email"].Value; //获取邮箱地址
string strFormat = String.Format("我是:{0},入职时间:{1},邮箱:{2}", userName, time, email);
result = regex.Replace(str, strFormat); //替换内容
Console.WriteLine(result);
}
return result; //结果:大家好!我是张三,入职时间:2010-10-7,邮箱:zhangsan@163.com 自我介绍。
}
/// <summary>
/// 从XML中提取匹配的字段
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
string strXml = GetStrXml(); //创建用户信息XML
Regex userRegex = new Regex(@"<User\s*EntryTime='(?<time>[\s\S]*?)'\s+Email='(?<email>[\s\S]*?)'>(?<userName>[\s\S]*?)</User>", RegexOptions.IgnoreCase);
MatchCollection userMatchColl = userRegex.Matches(strXml);
if (userMatchColl.Count > 0)
{
foreach (Match matchItem in userMatchColl)
{
string userName = matchItem.Groups["userName"].Value; //获取用户名
string time = TransDate(matchItem.Groups["time"].Value); //获取入职时间,并转换日期格式
string email = CheckMail(matchItem.Groups["email"].Value); //获取邮箱地址,并检测邮箱格式
string strFormat = String.Format("姓名:{0},入职时间:{1},邮箱:{2}", userName, time, email);
Console.WriteLine(strFormat);
}
}
Console.ReadLine();
}
/// <summary>
/// 创建用户信息XML
/// </summary>
/// <returns></returns>
public static string GetStrXml()
{
StringBuilder strXml = new StringBuilder();
strXml.Append("<UserInfo>");
strXml.Append("<User EntryTime='2010-10-7' Email='zhangsan@163.com'>张三</User>");
strXml.Append("<User EntryTime='2012-5-15' Email='lisi163.com'>李四</User>");
strXml.Append("<User EntryTime='2012-6-13' Email='wangwu@qq.com'>王五</User>");
strXml.Append("</UserInfo>");
return strXml.ToString();
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有