標籤:c#
C# Regex
理論:
Regex 是一種匹配輸入文本的模式。
.Net 架構提供了允許這種匹配的Regex引擎。
模式由一個或多個字元、運算子和結構組成。
定義Regex
下面列出了用於定義Regex的各種類別的字元、運算子和結構。
字元轉義 字元類 錨點 分組構造 限定符 反向引用構造 備用構造 替換 雜項構造
字元轉義 Regex中的反斜線字元(\)指示其後跟的字元是特殊字元,或應按原義解釋該字元.
定義Regex
下面列出了用於定義Regex的各種類別的字元、運算子和結構。
字元轉義
字元類
錨點
分組構造
限定符
反向引用構造
備用構造
替換
雜項構造
字元轉義
Regex中的反斜線字元(\)指示其後跟的字元是特殊字元,或應按原義解釋該字元。
下表列出了逸出字元:
650) this.width=650;" title="Capture.PNG" alt="wKioL1VoEzviA11hAAOX5T2jm6g742.jpg" src="http://s3.51cto.com/wyfs02/M02/6D/A0/wKioL1VoEzviA11hAAOX5T2jm6g742.jpg" />
字元類
字元類與一組字元中的任何一個字元匹配。
下表列出了字元類:
650) this.width=650;" title="Capture.PNG" alt="wKioL1VoE3TCJz0yAAO_WNKI3MQ665.jpg" src="http://s3.51cto.com/wyfs02/M00/6D/A0/wKioL1VoE3TCJz0yAAO_WNKI3MQ665.jpg" />
650) this.width=650;" title="Capture.PNG" alt="wKioL1VoEwzQEunOAALZSz7JHd0266.jpg" src="http://s3.51cto.com/wyfs02/M01/6D/A0/wKioL1VoEwzQEunOAALZSz7JHd0266.jpg" />
分組構造
分組構造描述了Regex的子運算式,通常用於捕獲輸入字串的子字串。
下表列出了分組構造:
650) this.width=650;" title="Capture.PNG" alt="wKiom1VoEVzwAX3RAAOxZSjFwE4789.jpg" src="http://s3.51cto.com/wyfs02/M00/6D/A4/wKiom1VoEVzwAX3RAAOxZSjFwE4789.jpg" />限定符
限定符指定在輸入字串中必須存在上一個元素(可以是字元、組或字元類)的多少個執行個體才能出現匹配項。 限定符包括下表中列出的語言元素。
下表列出了限定符:
650) this.width=650;" title="Capture2.PNG" style="float:none;" alt="wKiom1VoERnRqiCRAANzmOEDMFg126.jpg" src="http://s3.51cto.com/wyfs02/M02/6D/A4/wKiom1VoERnRqiCRAANzmOEDMFg126.jpg" />
650) this.width=650;" title="Capture.PNG" style="float:none;" alt="wKioL1VoEqySkHpcAAMosU9aE9A816.jpg" src="http://s3.51cto.com/wyfs02/M02/6D/A0/wKioL1VoEqySkHpcAAMosU9aE9A816.jpg" />
替換
替換是替換模式中使用的Regex。
下表列出了用於替換的字元:
650) this.width=650;" title="Capture2.PNG" style="float:none;" alt="wKiom1VoELeyoyswAAPHnOY8Hn4379.jpg" src="http://s3.51cto.com/wyfs02/M00/6D/A4/wKiom1VoELeyoyswAAPHnOY8Hn4379.jpg" />
650) this.width=650;" title="Capture.PNG" style="float:none;" alt="wKioL1VoEkryK8QxAAM1VBqzdDo432.jpg" src="http://s3.51cto.com/wyfs02/M00/6D/A0/wKioL1VoEkryK8QxAAM1VBqzdDo432.jpg" />
執行個體 1
下面的執行個體匹配了以 ‘S‘ 開頭的單詞:
using System;using System.Text.RegularExpressions;namespace RegExApplication{ class Program { private static void showMatch(string text, string expr) { Console.WriteLine("The Expression: " + expr); MatchCollection mc = Regex.Matches(text, expr); foreach (Match m in mc) { Console.WriteLine(m); } } static void Main(string[] args) { string str = "A Thousand Splendid Suns"; Console.WriteLine("Matching words that start with ‘S‘: "); showMatch(str, @"\bS\S*"); Console.ReadKey(); } }}
當上面的代碼被編譯和執行時,它會產生下列結果:
Matching words that start with ‘S‘:
The Expression: \bS\S*
Splendid
Suns
執行個體 2
下面的執行個體匹配了以 ‘m‘ 開頭以 ‘e‘ 結尾的單詞:
using System;using System.Text.RegularExpressions;namespace RegExApplication{ class Program { private static void showMatch(string text, string expr) { Console.WriteLine("The Expression: " + expr); MatchCollection mc = Regex.Matches(text, expr); foreach (Match m in mc) { Console.WriteLine(m); } } static void Main(string[] args) { string str = "make maze and manage to measure it"; Console.WriteLine("Matching words start with ‘m‘ and ends with ‘e‘:"); showMatch(str, @"\bm\S*e\b"); Console.ReadKey(); } }}
當上面的代碼被編譯和執行時,它會產生下列結果:
Matching words start with ‘m‘ and ends with ‘e‘:
The Expression: \bm\S*e\b
make
maze
manage
measure
執行個體 3
下面的執行個體替換掉多餘的空格:
using System;using System.Text.RegularExpressions;namespace RegExApplication{ class Program { static void Main(string[] args) { string input = "Hello World "; string pattern = "\\s+"; string replacement = " "; Regex rgx = new Regex(pattern); string result = rgx.Replace(input, replacement); Console.WriteLine("Original String: {0}", input); Console.WriteLine("Replacement String: {0}", result); Console.ReadKey(); } }}
當上面的代碼被編譯和執行時,它會產生下列結果:
Original String: Hello World
Replacement String: Hello World
參考:
http://outofmemory.cn/csharp/tutorial/csharp-regular-expressions.html
本文出自 “Ricky's Blog” 部落格,請務必保留此出處http://57388.blog.51cto.com/47388/1656348
49. C# -- Regex