源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

C#正则函数用法实例【匹配、替换、提取】

  • 时间:2020-10-06 01:56 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:C#正则函数用法实例【匹配、替换、提取】
本文实例讲述了C#正则函数用法。分享给大家供大家参考,具体如下: System.Text.RegularExpressions 命名空间包含一些类,这些类提供对 .NET Framework 正则表达式引擎的访问。该命名空间提供正则表达式功能,可以从运行在 Microsoft .NET Framework 内的任何平台或语言中使用该功能。 [b]1 正则表达式的常见使用[/b] ① 格式匹配
/// <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 自我介绍。
}

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

[b]PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:[/b] [b]JavaScript正则表达式在线测试工具: [/b][url=http://tools.jb51.net/regex/javascript]http://tools.jb51.net/regex/javascript[/url] [b]正则表达式在线生成工具: [/b][url=http://tools.jb51.net/regex/create_reg]http://tools.jb51.net/regex/create_reg[/url] 更多关于C#相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/344.htm]C#正则表达式用法总结[/url]》、《[url=http://www.1sucai.cn/Special/792.htm]C#编码操作技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/266.htm]C#中XML文件操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/165.htm]C#常见控件用法教程[/url]》、《[url=http://www.1sucai.cn/Special/125.htm]WinForm控件用法总结[/url]》、《[url=http://www.1sucai.cn/Special/116.htm]C#数据结构与算法教程[/url]》、《[url=http://www.1sucai.cn/Special/478.htm]C#面向对象程序设计入门教程[/url]》及《[url=http://www.1sucai.cn/Special/227.htm]C#程序设计之线程使用技巧总结[/url]》 希望本文所述对大家C#程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部