天天看點

正規表達式構造

正規表達式是正規表達式引擎嘗試比對輸入文本的一種模式。 模式由一個或多個字元文本、運算符或構造組成。

關于正規表達式可以參考MSDN:​​http://msdn.microsoft.com/zh-cn/library/az24scfc(v=vs.110).aspx​​

字元組

形式一般為[...],比對方括号中任意字元

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string pattern = @"[abc]";
            string inputs = "ab";
            Regex regex = new Regex(pattern);
            if (regex.IsMatch(inputs))
            {
                Console.WriteLine(inputs + " matches " + pattern);
            } 
            else
            {
                Console.WriteLine(inputs + " dose not match " + pattern);
            }
        }
    }
}      

運作結果:ab matches [abc]

[^...]比對非方括号中字元的任意字元

運作結果:good matche [^abc]

連字元'-'表示範圍,如[0123456789]等價于[0-9]

正規表達式提供常用的一些字元類:

[\d] = [0-9]

[\D] = [^0-9]

[\w] = [0-9a-zA-Z_]

[\W] = [^0-9a-zA-Z_]

[\s]比對空白字元

[\S]比對非空白字元

點号可以比對幾乎所有的字元(點号不能比對換行符)

\.比對點号本身

量詞(限定字元出現的次數)

*之前的字元可以出現0次到無窮多次{0,}

+之前的字元至少需要出現1次{1,}

?之前的字元多隻能出現1次{0,1}

區間量詞

{min, max} 比對上一個元素至少 min次,但不多于 max 次

{min, } 比對上一個元素至少 min 次

{number} 比對上一個元素恰好 number 次

如果要規定一個字元串的出現次數,必須使用(),在括号内填寫字元串,在閉括号之後添加量詞

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string pattern = @"\w{2}\d+";
            string inputs = "DF2";
            Regex regex = new Regex(pattern);
            if (regex.IsMatch(inputs))
            {
                Console.WriteLine(inputs + " matches " + pattern);
            } 
            else
            {
                Console.WriteLine(inputs + " dose not match " + pattern);
            }
        }
    }
}      

運作結果:DF2 matches \w{2}\d+

小括号的作用

1.多選結構,表示某個位置出現的字元串(…|…)

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string pattern = @"(good|bad)day";
            string inputs = "goodday";
            Regex regex = new Regex(pattern);
            if (regex.IsMatch(inputs))
            {
                Console.WriteLine(inputs + " matches " + pattern);
            } 
            else
            {
                Console.WriteLine(inputs + " dose not match " + pattern);
            }
        }
    }
}      

(good|bad)day既可以比對goodday又可以比對badday

2.捕獲分組,将括号内的子表達式捕獲的字元串存放到比對結果中,供比對完成後通路

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string pattern = @"(\w+)@(\w+\.\w+)";
            string inputs = "[email protected]";
            Regex regex = new Regex(pattern);
            if (regex.IsMatch(inputs))
            {
                Console.WriteLine(inputs + " matches " + pattern);
                MatchCollection collections = Regex.Matches(inputs, pattern);
                foreach (Match match in collections)
                {
                    Console.WriteLine("Local Address:  {0}", match.Groups[1].Value);
                    Console.WriteLine("Server Address: {0}", match.Groups[2].Value);
                    Console.WriteLine();
                }

            }
            else
            {
                Console.WriteLine(inputs + " dose not match " + pattern);
            }
        }
    }
}      

運作結果:

[email protected] matches (\w+)@(\w+\.\w+)

Local Address  :  theonegis

Server Address: qq.com

注意:

隻要使用了括号,就存在捕獲分組

捕獲分組按照開括号出現的從左至右的順序編号,遇到括号嵌套的情況也是如此

如果捕獲分組之後存在量詞,則比對結果中捕獲分組儲存的是子表達式最後一次比對的字元串

3.不捕獲文本的括号

如果正規表達式很複雜,或者需要處理的文本很長,捕獲分組會降低效率

僅僅用來對表達式分組,而不把分組捕獲的文本存入結果

形式(?:…)

4.反向引用,在表達式的某一部分,動态重複之前的子表達式所比對的文本

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string pattern = @"<(\w+)>[^<]+</(\1)>";
            string inputs = "<div>TheOneGIS</div>";
            Regex regex = new Regex(pattern);
            if (regex.IsMatch(inputs))
            {
                Console.WriteLine(inputs + " matches " + pattern);
            }
            else
            {
                Console.WriteLine(inputs + " dose not match " + pattern);
            }
        }
    }
}      

執行個體中第一個<div>比對<(\w+)>,TheOneGIS比對[^<]+,表示非<字元,後面的</div>比對</(\1)>,(\1)表示和第一個比對結果相同的比對。

錨點(規定比對的位置)

\b單詞分界符錨點

\b表示單詞分界符,要求一側是單詞字元,另一側是非單詞字元

單詞字元通常是指的是英文字元、數字字元,對中文不适用

非單詞字元通常指的是各種标點符号和空白字元

^比對一行的開頭

$比對一行的末尾

\A比對整個字元串的開頭

\Z比對整個字元串的末尾

環視

錨點對位置的判斷不夠靈活

應用子表達式對位置進行判斷

表達形式 名稱 作用
(?=…) 肯定順序環視 右側文本能由子表達式比對
(?!...) 否定順序環視 右側文本不能由子表達式比對
(?<…)  順序逆序環視 左側文本能由子表示比對
(?<!...) 否定逆序環視  左側文本不能由子表達式比對

環視結構僅用于布爾判斷,結構内的子表達式所比對的文本,不會儲存在整個表達式的比對結果之中

逆序環視結構對子表達式存在限制,.NET中沒有限制。

比對模式:改變某些結構的比對規則

I: Case Insensitive 不區分大小寫

S: Single Line (dot all)  點号通配