The car parked in the garage"The" => The car parked in the gara..._c# pattern">
当前位置:   article > 正文

C#中的正则表达式_c# pattern

c# pattern

一:什么是正则表达式

正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符以及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑,用来做检索或匹配。简写为regex


二:语法规则

正则表达式做检索判断一般都需要以^开头以$结尾,做替换的话不需要

——^和$:匹配开始和结束

  1. using System;
  2. using System.Text.RegularExpressions;
  3. class MainClass
  4. {
  5. static string str = "hello world";
  6. static void Main(string[] args)
  7. {
  8. string newStr1 = Regex.Replace(str, "^", "ha ");
  9. Console.WriteLine(newStr1); //ha hello world
  10. string newStr2 = Regex.Replace(str, "$", " ha");
  11. Console.WriteLine(newStr2); //hello world ha
  12. }
  13. }

——.:匹配除换行外的任意字符

  1. using System;
  2. using System.Text.RegularExpressions;
  3. class MainClass
  4. {
  5. static string str = "hello world";
  6. static void Main(string[] args)
  7. {
  8. string newStr = Regex.Replace(str, ".", "ha");
  9. Console.WriteLine(newStr); //hahahahahahahahahahaha
  10. }
  11. }

——*:匹配0个或多个字符,等价于{0,}

——?:匹配0个或1个字符,等价于{0,1}

——+:匹配1个或多个字符,等价于{1,}

——\w:匹配字母、数字、下划线、汉字字符

string pattern = @"^\w*$";

——\W:匹配除了字母、数字、下划线、汉字以外的字符

string pattern = @"^\W*$";

——\s:匹配任意空白字符(\n \r \t \f \v)

string pattern = @"^\s*$";

——\S:匹配除了任意空白字符(\n \r \t \f \v)以外的字符

string pattern = @"^\S*$";

——\d:匹配数字字符

string pattern = @"^\d*$";

——\D:匹配除了数字字符以外的字符

string pattern = @"^\D*$";

——[xyz]:匹配中括号中的字符 

  1. using System;
  2. using System.Text.RegularExpressions;
  3. class MainClass
  4. {
  5. static string str = "hello world";
  6. static void Main(string[] args)
  7. {
  8. string pattern = @"[l]";
  9. string newStr = Regex.Replace(str, pattern, "*");
  10. Console.WriteLine(newStr); //he**o wor*d
  11. }
  12. }

——[x-y]:匹配x-y之间的字符

  1. using System;
  2. using System.Text.RegularExpressions;
  3. class MainClass
  4. {
  5. static string str = "hello world";
  6. static void Main(string[] args)
  7. {
  8. string pattern = @"[a-d]";
  9. string newStr = Regex.Replace(str, pattern, "*");
  10. Console.WriteLine(newStr); //hello worl*
  11. }
  12. }

——[^xyz]:匹配除了中括号中的字符

  1. using System;
  2. using System.Text.RegularExpressions;
  3. class MainClass
  4. {
  5. static string str = "hello world";
  6. static void Main(string[] args)
  7. {
  8. string pattern = @"[^lh]";
  9. string newStr = Regex.Replace(str, pattern, "*");
  10. Console.WriteLine(newStr); //h*ll*****l*
  11. }
  12. }

——{n}:匹配前面的字符n次

string pattern = @"^\d{5}$";

——{n,}:匹配前面的字符n次或大于n次

string pattern = @"^\d{5,}$";

——{n,m}: 匹配前面的字符n-m次 

string pattern = @"^\d{5,10}$";

——x|y:匹配x或y的字符

string pattern = @"^\d|[a-z]$";

——():对正则表达式进行分组 

string pattern = @"^(\d|[a-z]){2}$";

三:常用的正则表达式

  1. //只包含数字,并且数字的个数=n
  2. Regex regex = new Regex(@"^\d{n}$");
  3. //只包含数字,并且数字的个数>=n
  4. Regex regex = new Regex(@"^\d{n,}$");
  5. //只包含数字,n<=并且数字的个数<=m
  6. Regex regex = new Regex(@"^\d{n,m}$");
  7. //只包含汉字
  8. Regex regex = new Regex(@"^[\u4e00-\u9fa5]{0,}$");
  9. //只包含英文和数字
  10. Regex regex = new Regex(@"^[A-Za-z0-9]+$");
  11. //长度为3-20的所有字符
  12. Regex regex = new Regex(@"^.{3,20}$");
  13. //只包含数字字母下划线
  14. Regex regex = new Regex(@"^\w+$");
  15. ====================
  16. //验证邮箱
  17. Regex regex = new Regex(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
  18. //验证手机号
  19. Regex regex = new Regex(@"^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$");
  20. //验证身份证
  21. Regex regex = new Regex(@"^\d{15}|\d{18}$");
  22. //验证密码(密码必须由字母开头,只能包含数字字母下划线,并且n+1<=密码位数<=m+1)
  23. Regex regex = new Regex(@"^[a-zA-Z]\w{n,m}$");
  24. ====================
  25. //匹配空白行
  26. Regex reg = new Regex(@"\n\s*\r");
  27. //匹配每行的首尾空白符
  28. Regex regex = new Regex(@"^\s*|\s*$");
  29. //匹配//注释
  30. Regex regex = new Regex("//.*\n");

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/56572
推荐阅读
相关标签
  

闽ICP备14008679号