偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

使用C#正則表達(dá)式匹配相關(guān)字符串

開發(fā) 后端
利用C#正則表達(dá)式匹配相關(guān)字符串,一般使用System.Text.RegularExpressions方法命名空間或使用Matches()方法匹配字符串。

C#正則表達(dá)式匹配字符串的方法如下:

1.使用C#中使用正則表達(dá)式System.Text.RegularExpressions命名空間;

2.使用C#中使用正則表達(dá)式Matches()方法匹配字符串,格式如下:
MatchCollection Matches = Regex.Matches(Str, Pattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);

其中Str表示輸入字符串,Pattern表示匹配模式,RegexOptions.IgnoreCase表示忽略大小寫,RegexOptions.ExplicitCapture表示改變收集匹配方式。

3.匹配結(jié)果保存在MatchCollection集合中,可以通過循環(huán)遍歷結(jié)果集取出Match對象的結(jié)果。

TestRagular.cs:

  1. using System;  
  2. using System.Text.RegularExpressions;  
  3.  
  4. namespace Magci.Test.Strings  
  5. {  
  6.     public class TestRegular  
  7.     {  
  8.         public static void WriteMatches(string str, MatchCollection matches)  
  9.         {  
  10.             Console.WriteLine("\nString is : " + str);  
  11.             Console.WriteLine("No. of matches : " + matches.Count);  
  12.             foreach (Match nextMatch in matches)  
  13.             {  
  14.                 //取出匹配字符串和最多10個外圍字符  
  15.                 int Index = nextMatch.Index;  
  16.                 string result = nextMatch.ToString();  
  17.                 int charsBefore = (Index < 5) ? Index : 5;  
  18.                 int fromEnd = str.Length - Index - result.Length;  
  19.                 int charsAfter = (fromEnd < 5) ? fromEnd : 5;  
  20.                 int charsToDisplay = charsBefore + result.Length + charsAfter;  
  21.  
  22.                 Console.WriteLine("Index: {0},\tString: {1},\t{2}", Index, result, str.Substring(Index - charsBefore, charsToDisplay));  
  23.             }  
  24.         }  
  25.  
  26.         public static void Main()  
  27.         {  
  28.             string Str = @"My name is Magci, for short mgc. I like c sharp!";  
  29.  
  30.             //查找“gc”  
  31.             string Pattern = "gc";  
  32.             MatchCollection Matches = Regex.Matches(Str, Pattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);  
  33.  
  34.             WriteMatches(Str, Matches);  
  35.               
  36.             //查找以“m”開頭,“c”結(jié)尾的單詞  
  37.             Pattern = @"\bm\S*c\b";  
  38.             Matches = Regex.Matches(Str, Pattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);  
  39.  
  40.             WriteMatches(Str, Matches);  
  41.         }  
  42.     }  

【編輯推薦】

  1. 介紹Mono C#編譯器
  2. C#運算符重載學(xué)習(xí)總結(jié)
  3. 概述C#語言的結(jié)構(gòu)體
  4. C#遺傳算法學(xué)習(xí)筆記
  5. 討論C#分部方法
責(zé)任編輯:彭凡 來源: mgc.ahau.edu.cn
相關(guān)推薦

2009-08-20 14:31:55

C#正則表達(dá)式字符串

2024-09-30 11:16:39

C#正則表達(dá)式

2009-09-16 17:02:15

正則表達(dá)式匹配字符串

2009-09-16 17:54:31

正則表達(dá)式實現(xiàn)

2009-08-07 15:16:10

C#正則表達(dá)式

2009-08-03 17:27:14

C#正則表達(dá)式

2009-08-20 13:38:58

C#正則表達(dá)式

2009-08-17 13:56:28

C#正則表達(dá)式入門

2009-08-20 13:26:35

C#正則表達(dá)式

2009-08-17 13:49:23

C#正則表達(dá)式提取

2009-08-14 16:50:59

C#正則表達(dá)式語法

2024-12-16 07:33:45

C#正則表達(dá)式

2009-08-20 16:13:32

C#正則表達(dá)式匹配

2009-08-20 13:30:38

C#正則表達(dá)式

2009-08-11 13:00:41

C#正則表達(dá)式

2009-08-13 15:24:27

C#正則表達(dá)式

2009-08-20 16:23:32

C#正則表達(dá)式語法

2009-08-24 17:14:41

正則表達(dá)式C#和.NET框架

2009-08-27 15:45:30

C#正則表達(dá)式

2009-08-20 14:43:03

C#正則表達(dá)式Rege
點贊
收藏

51CTO技術(shù)棧公眾號