1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 【C#进阶三】C#正则表达式的使用及常用案例(Regex.IsMatch Regex.Match match.Next

【C#进阶三】C#正则表达式的使用及常用案例(Regex.IsMatch Regex.Match match.Next

时间:2020-10-27 10:11:54

相关推荐

【C#进阶三】C#正则表达式的使用及常用案例(Regex.IsMatch Regex.Match match.Next

文章目录

1. 匹配正则表达式模式:Regex.IsMatch2.提取单个匹配项或第一个匹配项:Regex.Match(),match.NextMatch()3.提取所有匹配项Regex.Matches(),返回一个MatchCollection4.替换匹配的子字符串:Regex.Replace5.将单个字符串拆分成一个字符串数组Regex.Split6.Group集合7. 其它案例

C#进阶笔记系列上一篇总结了C#正则表达式的理论部分【C#进阶二】C#中的正则表达式知识总结(字符转义/字符类/ 定位点/ 分组构造 /数量词/反向引用构造/替换构造/替代/正则表达式选项)(理论篇)这一篇主要总结了C#正则表达式的使用及常用案例。

可以通过以下两种方式之一使用正则表达式引擎:

通过调用 Regex 类的静态方法。 方法参数包含输入字符串和正则表达式模式。 正则表达式引擎会缓存静态方法调用中使用的正则表达式,这样一来,重复调用使用同一正则表达式的静态正则表达式方法将提供相对良好的性能。通过实例化Regex 对象,采用的方式是将一个正则表达式传递给类构造函数。

可以调用 Regex类的方法来执行下列操作:

确定字符串是否与正则表达式模式匹配。提取单个匹配项或第一个匹配项。提取所有匹配项。替换匹配的子字符串。将单个字符串拆分成一个字符串数组。

1. 匹配正则表达式模式:Regex.IsMatch

public static void Main(){string[] values = { "111-22-3333", "111-2-3333"};string pattern = @"^\d{3}-\d{2}-\d{4}$";foreach (string value in values) {if (Regex.IsMatch(value, pattern)) //使用Regex.IsMatch()判断是否匹配了Console.WriteLine("{0} is a valid SSN.", value);elseConsole.WriteLine("{0}: Invalid", value);}}

正则表达式模式^\d{3}-\d{2}-\d{4}$的含义:

结果:

111-22-3333 is a valid SSN.111-2-3333: Invalid

2.提取单个匹配项或第一个匹配项:Regex.Match(),match.NextMatch()

public static void Main(){string input = "This is a a farm that that raises dairy cattle.";string pattern = @"\b(\w+)\W+(\1)\b";Match match = Regex.Match(input, pattern);while (match.Success){Console.WriteLine("Duplicate '{0}' found at position {1}.",match.Groups[1].Value, match.Groups[2].Index);match = match.NextMatch(); }}

正则表达式模式\b(\w+)\W+(\1)\b的含义:

结果:

Duplicate 'a' found at position 8.Duplicate 'that' found at position 17.

3.提取所有匹配项Regex.Matches(),返回一个MatchCollection

public static void Main(){string input = "This is a a farm that that raises dairy cattle.";string pattern = @"\b(\w+)\W+(\1)\b";foreach (Match match in Regex.Matches(input, pattern))Console.WriteLine("Duplicate '{0}' found at position {1}.",match.Groups[1].Value, match.Groups[2].Index);}

结果:

Duplicate 'a' found at position 8.Duplicate 'that' found at position 17.

4.替换匹配的子字符串:Regex.Replace

public static void Main(){string pattern = @"\b\d+\.\d{2}\b";string replacement = "$$$&";string input = "Total Cost: 103.64";Console.WriteLine(Regex.Replace(input, pattern, replacement));}

正则表达式模式\b\d+\.\d{2}\b的含义:

结果:

Total Cost: $103.64

5.将单个字符串拆分成一个字符串数组Regex.Split

public static void Main(){string input = "1. Eggs 2. Bread 3. Milk 4. Coffee 5. Tea";string pattern = @"\b\d{1,2}\.\s";foreach (string item in Regex.Split(input, pattern)){if (! String.IsNullOrEmpty(item))Console.WriteLine(item);}}

结果:

EggsBreadMilkCoffeeTea

6.Group集合

public static void Main(){string pattern = @"\b(?<月>\w+)\s(?<日>\d{1,2}),\s(?<年>\d{4})\b";string input = "Born: July 28, 1989 ; QHCV: May 12, 1999 ";MatchCollection matches = Regex.Matches(input, pattern);foreach (Match match in matches){Console.WriteLine("match: '{0}' found at position {1}.", match.Value, match.Index);foreach (Group group in match.Groups){Console.WriteLine("Group'{0}' found at position {1}.", group.Value, group.Index);foreach (Capture capture in group.Captures){Console.WriteLine(" capture: '{0}' found at position {1}.", capture.Value, capture.Index);}}} }

正则表达式模式\b(\w+)\s(\d{1,2}),\s(\d{4})\b的定义:

结果:

match: 'July 28, 1989' found at position 6.Group'July 28, 1989' found at position 6.capture: 'July 28, 1989' found at position 6.Group'July' found at position 6.capture: 'July' found at position 6.Group'28' found at position 11.capture: '28' found at position 11.Group'1989' found at position 15.capture: '1989' found at position 15.match: 'May 12, 1999' found at position 28.Group'May 12, 1999' found at position 28.capture: 'May 12, 1999' found at position 28.Group'May' found at position 28.capture: 'May' found at position 28.Group'12' found at position 32.capture: '12' found at position 32.Group'1999' found at position 36.capture: '1999' found at position 36.

7. 其它案例

1.案例一

Substring(param1,param2),用法

第一个参数是起始位置,这是位置是原字符串的初始位置:注意:是从该位置开始取,但是不包括这个点,

第二个参数是结束位置,可以省略,省略的话就是从开始位置一直取到最后,如果不省略,就是取得指定的长度如果超出了原字符串的长度,则报错,此举认为是个Bug。举个例子:

例如:“abcdefg”.Substring(3, 3) 结果:def

​ “abcdefg”.Substring(3, 5) 结果:报错

public static void Main(){Stopwatch sw; //可以准确的测量运行时间。string[] addresses = { "AAAAAAAAAAA@","AAAAAAAAAAaaaaaaaaaa!@" }; string pattern = @"^[0-9A-Z]([-.\w]*[0-9A-Z])*$"; //正则表达式string input;foreach (var address in addresses){string mailBox = address.Substring(0, address.IndexOf("@")); int index = 0;for (int ctr = mailBox.Length - 1; ctr >= 0; ctr--){index++;input = mailBox.Substring(ctr, index);sw = Stopwatch.StartNew(); //设置开始Match m = Regex.Match(input, pattern, RegexOptions.IgnoreCase); //RegexOptions.IgnoreCase忽略大小写sw.Stop(); //设置结束if (m.Success)Console.WriteLine("{0,2}. Matched '{1,-25}' in {2}",index, m.Value, sw.Elapsed);elseConsole.WriteLine("{0,2}. Failed '{1,-25}' in {2}",index, input, sw.Elapsed);}Console.WriteLine();}}

结果:

1. Matched 'A ' in 00:00:00.14538422. Matched 'AA ' in 00:00:00.00000853. Matched 'AAA ' in 00:00:00.00000234. Matched 'AAAA ' in 00:00:00.00000195. Matched 'AAAAA' in 00:00:00.00000206. Matched 'AAAAAA ' in 00:00:00.00000207. Matched 'AAAAAAA ' in 00:00:00.00000218. Matched 'AAAAAAAA ' in 00:00:00.00000209. Matched 'AAAAAAAAA' in 00:00:00.00000. Matched 'AAAAAAAAAA' in 00:00:00.000002111. Matched 'AAAAAAAAAAA ' in 00:00:00.00000211. Failed '! ' in 00:00:00.00000622. Failed 'a! ' in 00:00:00.00000453. Failed 'aa! ' in 00:00:00.00000274. Failed 'aaa! ' in 00:00:00.00000255. Failed 'aaaa!' in 00:00:00.00000286. Failed 'aaaaa! ' in 00:00:00.00000387. Failed 'aaaaaa! ' in 00:00:00.00001718. Failed 'aaaaaaa! ' in 00:00:00.00001029. Failed 'aaaaaaaa!' in 00:00:00.000018310. Failed 'aaaaaaaaa!' in 00:00:00.000035011. Failed 'aaaaaaaaaa! ' in 00:00:00.000068212. Failed 'Aaaaaaaaaaa! ' in 00:00:00.000135013. Failed 'AAaaaaaaaaaa! ' in 00:00:00.000269414. Failed 'AAAaaaaaaaaaa! ' in 00:00:00.000538115. Failed 'AAAAaaaaaaaaaa!' in 00:00:00.001374716. Failed 'AAAAAaaaaaaaaaa! ' in 00:00:00.002559817. Failed 'AAAAAAaaaaaaaaaa! ' in 00:00:00.005895118. Failed 'AAAAAAAaaaaaaaaaa! ' in 00:00:00.010230719. Failed 'AAAAAAAAaaaaaaaaaa!' in 00:00:00.022325320. Failed 'AAAAAAAAAaaaaaaaaaa!' in 00:00:00.035805121. Failed 'AAAAAAAAAAaaaaaaaaaa! ' in 00:00:00.0705792//尽管此正则表达式可以轻松处理有效输入,但在处理接近有效的输入时性能非常低效。

2.案例二

正则表达式模式\b(\w+((\r?\n)|,?\s))*\w+[.?:;!]的定义:

3.案例三

public static void Main(){string pattern = @"^[0-9A-Z][-.\w]*(?<=[0-9A-Z])\$$";string[] partNos = { "A1C$", "A4", "A4$", "A1603D$", "A1603D#" };foreach (var input in partNos) {Match match = Regex.Match(input, pattern);if (match.Success)Console.WriteLine(match.Value);elseConsole.WriteLine("Match not found.");}}

正则表达式^[0-9A-Z][-.\w]*(?<=[0-9A-Z])\$$的定义:

结果:

A1C$未匹配成功A4$A1603D$未匹配成功

4.案例四

public static void Main(){string input = "This is one sentence. This is another.";string pattern = @"\b(?:\w+[;,]?\s?)+[.?!]";foreach (Match match in Regex.Matches(input, pattern)) {Console.WriteLine("Match: '{0}' at index {1}.",match.Value, match.Index);int grpCtr = 0;foreach (Group grp in match.Groups) {Console.WriteLine(" Group {0}: '{1}' at index {2}.",grpCtr, grp.Value, grp.Index);int capCtr = 0;foreach (Capture cap in grp.Captures) {Console.WriteLine("Capture {0}: '{1}' at {2}.",capCtr, cap.Value, cap.Index);capCtr++;}grpCtr++;}Console.WriteLine();}}

正则表达式\b(?:\w+[;,]?\s?)+[.?!]的定义:

结果:

Match: 'This is one sentence.' at index 0.Group 0: 'This is one sentence.' at index 0.Capture 0: 'This is one sentence.' at 0.Match: 'This is another.' at index 22.Group 0: 'This is another.' at index 22.Capture 0: 'This is another.' at 22.

更多示例请看:正则表达式示例:扫描 HREF | Microsoft Docs

C#正则表达部分的内容就结束了!

参考资料:正则表达式对象模型 | Microsoft Docs

欢迎关注个人微信公众号【智能建造小硕】(分享计算机编程、人工智能、智能建造、日常学习和科研经验等,欢迎大家关注交流。)

【C#进阶三】C#正则表达式的使用及常用案例(Regex.IsMatch Regex.Match match.NextMatch Regex.Matches Regex.Replace等)(实践篇)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。