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 =@"www.baidu.com \n lkjsdflkj";


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章