C#正则函数的使用

System.Text.RegularExpressions 命名空间包含一些类,这些类提供对 .NET Framework 正则表达式引擎的访问。该命名空间提供正则表达式功能,可以从运行在 Microsoft .NET Framework 内的任何平台或语言中使用该功能。

1 正则表达式的常见使用

1.1 格式匹配

/// <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;
}

1.2 替换匹配内容

/// <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;
}

1.3 提取匹配内容

/// <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 自我介绍。
}

2 我的一个实例
/// <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();
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

pan_junbiao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值