鍍金池/ 教程/ C#/ C#正則表達式
C#屬性(Properties)
C#與Java比較
C#方法
C#枚舉
C#關(guān)鍵字
C# StreamReader類
C#不安全代碼
C#文件(I/O)
C#匿名方法
C#線程同步
C# Thread類
C#主線程
C#數(shù)據(jù)類型
C# FileStream類
C#預(yù)處理指令
C#繼承
C#循環(huán)
C#決策結(jié)構(gòu)
C#集合
C#反射
C#類型轉(zhuǎn)換
C#泛型
C# StringReader類
C#歷史
C#運算符重載
C#屬性
C#線程實例:Sleep()方法
C#線程示例:優(yōu)先級
C#線程實例:Join()方法
C# BinaryReader類
C#類
C#索引器
C# BinaryWriter類
C#序列化
C#常量和文字
C#程序結(jié)構(gòu)
C#封裝
C#事件
C#可空類型(nullable)
C#基本語法
C#異常處理
C#教程
C#接口
C# System.IO命名空間
C#線程命名實例
C# StringWriter類
C#線程實例
C#數(shù)組
C#正則表達式
C#命名空間
C#反序列化
C#與C++比較
C# TextWriter類
C#多線程
C#字符串
C#是什么?
C#變量
C# FileInfo類
C#線程實例:Abort()方法
C#結(jié)構(gòu)體
C#運算符
C#入門程序
C#多線程生命周期
C# TextReader類
C# DirectoryInfo類
C#委托

C#正則表達式

正則表達式是匹配輸入文本的模式。.Net框架提供了允許這種匹配的正則表達式引擎。模式由一個或多個字符文字,運算符或構(gòu)造組成。

定義正則表達式的構(gòu)造

有各種類型的字符,運算符和結(jié)構(gòu),可以讓您用來定義正則表達式。點擊以下鏈接查找這些結(jié)構(gòu)。

Regex類

正則表達式 - Regex 類用于表示正則表達式。 它有以下常用的方法:

序號 方法 描述
1 public bool IsMatch(string input) 指示在正則表達式構(gòu)造函數(shù)中指定的正則表達式是否在指定的輸入字符串中找到匹配項。
2 public bool IsMatch(string input, int startat) 指示在正則表達式構(gòu)造函數(shù)中指定的正則表達式是否在指定的輸入字符串(input)中找到匹配,從字符串中指定的起始(startat)位置開始。
3 public static bool IsMatch(string input, string pattern) 在指定的正則表達式是否在指定的輸入字符串中找到匹配項。
4 public MatchCollection Matches(string input) 搜索所有出現(xiàn)正則表達式的指定輸入字符串。
5 public string Replace(string input, string replacement) 在指定的輸入字符串中,將與正則表達式模式匹配的所有字符串替換為指定的替換字符串(replacementreplacement)。
6 public string[] Split(string input) 將輸入字符串拆分為由正則表達式構(gòu)造函數(shù)中指定的正則表達式模式定義的位置的子字符串數(shù)組。

有關(guān)方法和屬性的完整列表,請閱讀Microsoft C# 文檔。

實例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();
      }
   }
}

當編譯和執(zhí)行上述代碼時,會產(chǎn)生以下結(jié)果:

Matching words that start with 'S':
The Expression: \bS\S*
Splendid
Suns

示例2

以下示例匹配以'm'開頭并以'e'結(jié)尾的單詞:

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();
      }
   }
}

當編譯和執(zhí)行上述代碼時,會產(chǎn)生以下結(jié)果:

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();
      }
   }
}

當編譯和執(zhí)行上述代碼時,會產(chǎn)生以下結(jié)果:

Original String: Hello World   
Replacement String: Hello World