成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

使用C#正則表達式匹配相關字符串

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

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

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

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

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

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

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”結尾的單詞  
  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#運算符重載學習總結
  3. 概述C#語言的結構體
  4. C#遺傳算法學習筆記
  5. 討論C#分部方法
責任編輯:彭凡 來源: mgc.ahau.edu.cn
相關推薦

2009-08-20 14:31:55

C#正則表達式字符串

2024-09-30 11:16:39

C#正則表達式

2009-09-16 17:02:15

正則表達式匹配字符串

2009-08-20 13:38:58

C#正則表達式

2009-08-03 17:27:14

C#正則表達式

2009-08-07 15:16:10

C#正則表達式

2009-09-16 17:54:31

正則表達式實現

2009-08-17 13:56:28

C#正則表達式入門

2009-08-20 13:26:35

C#正則表達式

2009-08-20 16:13:32

C#正則表達式匹配

2009-08-17 13:49:23

C#正則表達式提取

2009-08-14 16:50:59

C#正則表達式語法

2024-12-16 07:33:45

C#正則表達式

2009-08-11 13:00:41

C#正則表達式

2009-08-13 15:24:27

C#正則表達式

2009-08-20 13:30:38

C#正則表達式

2010-03-15 16:21:28

Python正則表達式

2009-08-20 16:23:32

C#正則表達式語法

2009-08-24 17:14:41

正則表達式C#和.NET框架

2009-08-27 15:45:30

C#正則表達式
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 在线视频国产一区 | 日韩视频一区在线观看 | 日本a级大片 | 午夜性色a√在线视频观看9 | 夜夜夜久久久 | 99精品视频免费观看 | 国产一级特黄aaa大片评分 | 在线视频一区二区 | 久久伊人影院 | 国产精品大片在线观看 | 亚洲一级黄色 | 欧美精品综合 | 亚洲精品久久久久久久久久吃药 | 国产精品久久久久久亚洲调教 | 久久久国产精品网站 | 日韩成人在线观看 | 二区三区在线观看 | 午夜色播| 欧美男人天堂 | 91久久夜色 | 日产精品久久久一区二区福利 | 综合久久一区 | 伊人网国产 | 亚洲国产视频一区二区 | 亚洲欧美日韩高清 | 欧美日韩高清一区 | 中文字幕精品一区 | 国产精品久久久久影院色老大 | 成人av一区二区三区 | 亚洲永久入口 | 福利网址| 国产人久久人人人人爽 | www.亚洲区| 午夜精品一区二区三区免费视频 | 91亚洲国产成人精品一区二三 | 国产欧美精品 | 中文字幕国产一区 | 超碰人人插 | 日韩欧美在线播放 | 欧美三级电影在线播放 | 精品一区二区电影 |