vbscriptRegex

來源:互聯網
上載者:User

-------------------Regex的所有內容-----------------------------
RegExp屬性
   Global
   IgnoreCase
   Pattern

RegExp方法
   Execute      --對指定的字串執行Regex搜尋。
   Replace      --替換Regex搜尋到的字元。
   Test

RegExp對象
   Match
   Match屬性
       FirstIndex
       Length
       Value

RegExp集合
   Matches
   SubMatches

說明:

Regex搜尋的設計模式是通過 RegExp 對象的 Pattern 來設定的。

Execute 方法返回一個 Matches 集合,其中包含了在 string 中找到的每一個匹配的 Match 對象。如果未找到匹配,Execute 將返回空的 Matches 集合。

舉例:

Function RegExpTest(patrn, strng)  Dim regEx, Match, Matches  ‘建立變數  Set regEx = New RegExp    ‘建立Regex  regEx.Pattern = patrn         ‘設定搜尋方法  regEx.IgnoreCase = True     ‘設定是否區分大小寫  regEx.Global = True         ‘設定全程可用性  Set Matches = regEx.Execute(strng) ‘根據Regex規則執行搜尋字串  For Each Match in Matches             ‘遍曆Matches集合      RetStr = RetStr & Match.Value & ","   ‘顯示符合Regex規則的字元,此句也可寫為:            RetStr = RetStr &  Match   Next  RegExpTest = RetStrEnd Function str = RegExpTest("\d+", "xxafaf12dfasf3433432xx你好")msgbox str顯示:12,3433432
View Code

Function RegExpTest(patrn, strng)   Dim regEx, Match, Matches      Set regEx = New RegExp      regEx.Pattern = patrn      regEx.IgnoreCase = True      regEx.Global = True      Set Matches = regEx.Execute(strng)      For Each Match in Matches          RetStr = RetStr & Match.value   Next   RegExpTest = RetStrEnd Functionmsgbox RegExpTest("\d+","sdfsdfsd234")

   在這個樣本中我們可以看到方法Execute和對象Match是使用在集合Matches中的,Match和Matches是作為變數來使用的,為了我們很容易得看懂它,我們沒有必要將他們換名字。關於對象Match的屬性,我在上面的樣本中都做了注釋了。

RegExp的Replace方法介紹:

Replace--替換在Regex搜尋到的文本。

object.Replace(string1, string2)

參數:

object

Required. Always the name of a RegExp object.

string1

Required. String1 is the text string in which the text replacement is to occur.

string2

Required. String2 is the replacement text string.

說明:

被替換的文本的實際模式是通過 RegExp 對象的 Pattern 屬性設定的。

Replace 方法返回 string1 的副本,其中的 RegExp.Pattern 文本已經被替換為 string2。如果沒有找到匹配的文本,將返回原來的 string1 的副本。 

下面的例子說明了Replace方法的用法。

 

Function ReplaceTest(patrn,replStr)    Dim regEx,str1    str1 = “the quick browm fox jumps over the lazy dog.”    ‘create regular expression    Set regEx = New RegExp    regEx.Pattern = patrn    regEx.IgnoreCase = True    ‘make replacement.    ReplaceTest = regEx.Replace(str1,replStr)End FunctionMsgbox (ReplaceTest(“fox”,”cat”))   ‘replace ‘fox’ with ‘cat’.

RegExp的Test方法介紹:

Test 方法

對指定的字串執行一個Regex搜尋,並返回一個 Boolean 值指示是否找到匹配的模式。

object.Test(string)

參數

object

必選項。總是一個 RegExp 對象的名稱。

string

必選項。要執行Regex搜尋的文本字串。

說明

Regex搜尋的實際模式是通過RegExp對象的Pattern屬性來設定的。RegExp.Global屬性對Test方法沒有影響。

如果找到了匹配的模式,Test方法返回True;否則返回False。

下面的代碼說明了Test 方法的用法。

Function RegExpTest(patrn, strng)Dim regEx, retVal            ' 建立變數。Set regEx = New RegExp         ' 建立Regex。regEx.Pattern = patrn         ' 設定模式。     regEx.IgnoreCase = False         ' 設定是否區分大小寫。retVal = regEx.Test(strng)         ' 執行搜尋測試。      If retVal Then            RegExpTest = "找到一個或多個匹配。"      Else            RegExpTest = "未找到匹配。"     End IfEnd FunctionMsgBox(RegExpTest("is.", "IS1 is2 IS3 is4"))

SubMatches 集合

Regex子匹配字串的集合。

說明

SubMatches 集合包含了單個的子匹配字串,只能用 RegExp 對象的 Execute 方法建立。SubMatches 集合的屬性是唯讀。

運行一個Regex時,當圓括弧中捕捉到子運算式時可以有零個或多個子匹配。SubMatches 集合中的每一項是由Regex找到並捕獲的的字串。

下面的代碼示範了如何從一個Regex獲得一個 SubMatches 集合以及如何它的專有成員:

Function SubMatchTest(inpStr)  Dim oRe, oMatch, oMatches  Set oRe = New RegExp  '尋找一個電子郵件地址(不是一個理想的 RegExp)  oRe.Pattern = "(\w+)@(\w+)\.(\w+)"   ‘得到 Matches 集合    Set oMatches = oRe.Execute(inpStr)   ‘得到 Matches 集合中的第一項    Set oMatch = oMatches(0)   ‘ 建立結果字串。    Match 對象是完整匹配 — dragon@xyzzy.com   retStr = "電子郵件地址是: " & oMatch & vbNewline   ‘ 得到地址的子匹配部分。    retStr = retStr & "電子郵件別名是: " & oMatch.SubMatches(0)  ' dragon  retStr = retStr & vbNewline  retStr = retStr & "組織是: " & oMatch. SubMatches(1)' xyzzy  SubMatchTest = retStrEnd Function
MsgBox(SubMatchTest("請寫信到 dragon@xyzzy.com 。 謝謝!"))

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.