1. 程式人生 > >107-正則表示式介紹

107-正則表示式介紹

什麼是正則表示式?
英文Regular Expression,是電腦科學的一個重要概念,她使用一種數學演算法來解決計算機程式中的文字檢索,匹配等問題,正則表示式語言是一種專門用於字串處理的語言。在很多語言中都提供了對它的支援,c#也不例外,它可以幫我們解決下面的問題:
    1,檢索:通過正則表示式,從字串中獲取我們想要的部分
    2,匹配:判斷給定的字串是否符合正則表示式的過濾邏輯

 

可以認為正則表示式表述了一個字串的書寫規則。

 

我們可以用正則表示式判斷使用者輸入的密碼是否合法,判斷使用者輸入的郵箱格式是否合法等等。

正則表示式的組成

正則表示式就是由普通字元以及特殊字元(稱為元字元)組成的文字模式。該模式描述在查詢文字主體時待匹配的一個或多個字串。

常用的操作正則表示式的方法和委託

下面學習一下位於System.Text.RegularExpressions下的Regex類的一些靜態方法和委託
1,靜態方法IsMatch (返回值是一個布林型別,用於判斷指定的字串是否與正則表示式字串匹配,它有三個過載方法)
bool IsMatch(string input, string pattern);
引數:  input:       要搜尋匹配項的字串。
pattern:     要匹配的正則表示式模式。
返回結果:  如果正則表示式找到匹配項,則為 true;否則,為 false。
bool IsMatch(string input, string pattern, RegexOptions options);
引數:  input:       要搜尋匹配項的字串。
pattern:     要匹配的正則表示式模式。
options:     列舉值的一個按位組合,這些列舉值提供匹配選項。
返回結果:  如果正則表示式找到匹配項,則為 true;否則,為 false。
bool IsMatch(string input, string pattern, RegexOptions options, TimeSpan matchTimeout);
引數:  input:        要搜尋匹配項的字串。
pattern:      要匹配的正則表示式模式。
options:      列舉值的一個按位組合,這些列舉值提供匹配選項。
matchTimeout: 超時間隔,或 System.Text.RegularExpressions.Regex.InfiniteMatchTimeout 指示該方法不應超時。
返回結果:  如果正則表示式找到匹配項,則為 true;否則,為 false。

 

關於引數RegexOptions

它是一個列舉型別,有以下列舉值

 

  RegexOptions列舉值 內聯標誌 簡單說明
ExplicitCapture 只有定義了命名或編號的組才捕獲
IgnoreCase 不區分大小寫
IgnorePatternWhitespace x 消除模式中的非轉義空白並啟用由 # 標記的註釋。
MultiLine m 多行模式,其原理是修改了^和$的含義
SingleLine s 單行模式,和MultiLine相對應

內聯標誌可以更小力度(一組為單位)的定義匹配選項

 

靜態方法Match(System.Text.RegularExpressions)

 

靜態方法Match,使用指定的匹配選項在輸入字串中搜索指定的正則表示式的第一個匹配項。 返回一個包含有關匹配的資訊的物件。同樣有三個過載方法,引數和IsMatch方法相同。此外,在Regex類中,還有一個同名的非靜態方法,適用於多個例項的情況下,效率更高一些。

Match Match(string input, string pattern);

Match Match(string input, string pattern, RegexOptions options);

Match Match(string input, string pattern, RegexOptions options, TimeSpan matchTimeout);

靜態方法Matches(System.Text.RegularExpressions)

靜態方法Matches,在指定的輸入字串中搜索指定的正則表示式的所有匹配項。跟上面方法不同之處,就是這個方法返回的是所有匹配項,他同樣有三個過載方法,並且引數和Match方法完全相同
  MatchCollection Matches(string input, string pattern);
  MatchCollection Matches(string input, string pattern, RegexOptions options);
  MatchCollection Matches(string input, string pattern, RegexOptions options, TimeSpan matchTimeout);

Replaces函式(System.Text.RegularExpressions)

我們知道正則表示式主要是實現驗證,提取,分割,替換字元的功能.Replace函式是實現替換功能的.
1 )Replace(string input,string pattern,string replacement)  
//input是源字串,pattern是匹配的條件,replacement是替換的內容,就是把符合匹配條件pattern的內容轉換成它
比如string result = Regex.Replace("abc", "ab", "##");  
//結果是##c,就是把字串abc中的ab替換成##
2 )Replace(string input,string pattern,string replacement,RegexOptions options)      
//RegexOptions是一個列舉型別,用來做一些設定.
//前面用註釋時就用到了RegexOptions.IgnorePatternWhitespace.如果在匹配時忽略大小寫就可以用RegexOptions.IgnoreCase
比如string result = Regex.Replace("ABc", "ab", "##",RegexOptions.IgnoreCase);
如果是簡單的替換用上面兩個函式就可以實現了.但如果有些複雜的替換,比如匹配到很多內容,不同的內容要替換成不同的字元.就需要用到下面兩個函式
3 )Replace(string input,string pattern,MatchEvaluator evaluator);    
//evaluator是一個代理,其實簡單的說是一個函式指標,把一個函式做為引數參進來
//由於C#裡沒有指標就用代理來實現類似的功能.你可以用代理繫結的函式來指定你要實現的複雜替換.
4 )Replace(string input,string pattern,MatchEvaluator evaluator,RegexOptions options);
//這個函式上上面的功能一樣,只不過多了一點列舉型別來指定是否忽略大小寫等設定

靜態方法Split拆分文字

使用正則表示式匹配的位置,將文字拆分為一個字串陣列,同樣有三個過載方法,返回值為字串陣列
string[] Split(string input, string pattern);
string[] Split(string input, string pattern, RegexOptions options);
string[] Split(string input, string pattern, RegexOptions options, TimeSpanmatchTimeout);

@符號 

我們經常在正則表示式字串前面加上@字元,這樣不讓編譯器去解析其中的轉義字元,而作為正則表示式的語法(元字元)存在。


string s [email protected]"www.baidu.com \n lkjsdflkj";