鍍金池/ 教程/ C#/ 正則表達式
循環(huán)
正則表達式
概述
委托
多態(tài)性
字符串
繼承
結(jié)構(gòu)體
集合
變量
不安全代碼
判斷
反射
異常處理
可空類型
方法
數(shù)據(jù)類型
命名空間
文件 I/O
類型轉(zhuǎn)換
屬性
程序結(jié)構(gòu)
事件
接口
預(yù)處理指令
運算符
多線程
匿名方法
索引器
泛型
封裝
常量和文字
基本語法
特性
數(shù)組
環(huán)境配置
運算符重載
枚舉

正則表達式

正則表達式是一種可以和輸入文本相匹配的表達式。.Net framework 提供了一個正則表達式引擎讓這種匹配成為可能。一個表達式可以由一個或多個字符,運算符,或結(jié)構(gòu)體組成。

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

有很多種類的字符,運算符,結(jié)構(gòu)體可以定義正則表達式。

  • 轉(zhuǎn)義字符
  • 字符類
  • 集合
  • 分組構(gòu)造
  • 限定符
  • 回溯引用構(gòu)造
  • 可替換結(jié)構(gòu)
  • 替換
  • 混合結(jié)構(gòu)

Regex 正則表達式類

Regex 類用于表示一個正則表達式。它有下列常用的方法:

Sr.No 方法
1 public bool IsMatch(string input)
指出輸入的字符串中是否含有特定的正則表達式。
2 public bool IsMatch(string input, int startat)
指出輸入的字符串中是否含有特定的正則表達式,該函數(shù)的 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)
在一個特定的輸入字符中,用特定的字符串替換所有滿足某個表達式的字符串。
6 public string[] Split(string input)
將一個輸入字符拆分成一組子字符串,從一個由正則表達式指出的位置上開始。

有關(guān)屬性和方法的完成列表,請參見微軟的 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í)行上述代碼,得到如下結(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í)行上述代碼,得到如下結(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í)行上述代碼,得到如下結(jié)果:

    Original String: Hello World   
    Replacement String: Hello World
上一篇:概述下一篇:事件