C#正则函数匹配、替换、提取的用法代码分享


本文摘自PHP中文网,作者黄舟,侵删。

这篇文章主要介绍了C#正则函数用法,结合实例形式分析了C#基于正则的匹配、替换、提取相关操作技巧,需要的朋友可以参考下

本文实例讲述了C#正则函数用法。分享给大家供大家参考,具体如下:

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

1 正则表达式的常见使用

① 格式匹配

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

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

3

4

5

6

7

8

9

10

11

12

13

14

15

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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

/// <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 我的一个实例

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

/// <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();

}

以上就是C#正则函数匹配、替换、提取的用法代码分享的详细内容!

相关阅读 >>

C#实现检索不区分大小写并高亮显示的示例代码分享

详解C#不登录电脑启动程序(图文)

C#基于正则表达式抓取a标签链接和innerhtml的方法

C#中抽象类和接口的区别

最新使用C#生成二维码方案,详解及实例 ( qrcoder )

C#控制台应用程序中如何输出彩色字体的详细介绍

详解介绍C#是如何获取文件md5与sha1

C#如何计算2个字符串相似度的示例代码分享

C#基础入门-常量详解

浅谈C#方法的六种参数

更多相关阅读请进入《C#》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...