c#中的正则表达式的源码

xiaoxiao2021-02-28  86

//正则表达式 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; //正则表达式需要引用的 using System.Text.RegularExpressions; namespace Reflection {          class Program     {         //1.\d表示匹配数字,\D匹配所有的非数字. 表示匹配任意字符,\. 匹配.         //2.[]表示方括号里面的每一个字符或者数字都可以匹配         //3.c{3}表示匹配3个c,c{2,3}表示匹配最少2个c,最多3个c         //4.*表示0个或者多个,+表示一个或者多个,?表示有或者没有         //5.\s表示匹配空格,\S表示匹配非空格,\w匹配所有的字符,\W匹配特殊字符         //6.正则表达式的卡头^,结束为$         //7.()表示匹配括号里面的字符         //8.\b表示字符的边界         static void Main(string[] args)         {             RegexIsMatch();             RegexMatch();             RegexReplace();             Console.ReadLine();         }         //isMatch用法         private static void RegexIsMatch()         {             string[] values = { "111-22-3333", "111-2-3333" };             string pattern = @"^\d{3}-\d{2}-\d{4}$";             foreach (var value in values)             {                 if (Regex.IsMatch(value, pattern))                 {                     Console.WriteLine("{0} is valid", value);                 }                 else                 {                     Console.WriteLine("{0} is not valid", value);                 }             }         }         private static void RegexMatch()         {             var input = "This is cdc cdc!";             var pattern = @"\b(\w+)\W(\1)\b";             Match match = Regex.Match(input, pattern);             while (match.Success)             {                 Console.WriteLine("Duplication {} found", match.Groups[1].Value);                 match = match.NextMatch();             }         }         //正则表达式实现替换         private static void RegexReplace()         {             string patten = @"\b\d+\.\d{2}\b";             //最后一个$&表示匹配上的字符             string replacement = "$$$&";             string input = "total cost:103.64";             Console.WriteLine(Regex.Replace(input, patten, replacement));         }         //正则表达式实现分割         private static void RegexSplit()         {             string input = "1. Egg 2. Bread 3. Mike";             string patten = @"\b\d{1,2}\.\s";             foreach (string item in Regex.Split(input, patten))             {                 if (!string.IsNullOrEmpty(item))                 {                     Console.WriteLine(item);                 }             }         }         private static void Matches()         {             MatchCollection matches;             Regex r = new Regex("abc");             matches = r.Matches("123abc4abcd");//获得集合             foreach (Match match in matches)             {                 Console.WriteLine("{0} found at position {1}", match.Value, match.Index);                 Console.WriteLine("{0}", match.Result("$&, hello,cdc!"));//$&表示匹配的字符             }         }         //mach.Groups的使用         private static void Groups()         {             string input = "Born: July 28, 1989";             string patten = @"\b(\w)\s(\d{1,2}),\s(\d{4})\b";             Match match = Regex.Match(input, patten);             if (match.Success)             {                 for (int ctr = 0; ctr < match.Groups.Count; ctr++)                 {                     Console.WriteLine("Group{0}:{1}", ctr, match.Groups[ctr].Value);                 }             }         }     } }
转载请注明原文地址: https://www.6miu.com/read-96783.html

最新回复(0)