欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

VBS中的正則表達(dá)式的用法大全 原創(chuàng)

原創(chuàng)  更新時間:2006年10月27日 00:00:00   原創(chuàng) 作者:  
這篇文章主要為大家介紹下VBS中的正則表達(dá)式的一些使用方法,需要的朋友可以參考下
VBS正則表達(dá)式函數(shù)
主要用在asp中效果明顯 
1、表單驗證功能
復(fù)制代碼 代碼如下:
Function Validate(strng,patrn) 
 Dim regEx 
 Set regEx = New RegExp 
 regEx.Pattern = patrn 
 regEx.IgnoreCase = True 
 regEx.Global = True 
 Validate = regEx.test(strng) 
 Set regEx = Nothing 
End Function 


使用例子

  If Validate(Fdr.Name,"F\d{4}_P\d{4}")=True Then
     ... ...
  End If

[Ctrl+A 全選 注:引入外部Js需再刷新一下頁面才能執(zhí)行]

2、替換功能
復(fù)制代碼 代碼如下:

'==========================
'用正則表達(dá)式實現(xiàn)替換
'==========================
function replaceregex(patern,str,tagstr)
dim regex,matches
set regex=new regExp
regex.pattern=patern
regex.IgnoreCase=true
regex.global=true
matches=regex.replace(str,tagstr)
replaceregex=matches
end function

3、ubb功能
http://www.dbjr.com.cn/html/200608/6/574.htm

以下是 作者:LCX的內(nèi)容

以前一直沒有好好的學(xué)過,這次整理一下。

正則中/d+就是代表一個或多個數(shù)字,用這個做例子。

RegExp就是建立正則的對像。如Set regEx = New RegExp。regEx.Pattern 就是來設(shè)置正則的模式的,如

regEx.Pattern ="/d+"。regEx.IgnoreCase = True   ' 設(shè)置是否區(qū)分大小寫。regEx.Global = True   ' 設(shè)置全程可用性。

RegExp對像有3種方法,分別是execute、test、replace。

test方法是對指定的字符串執(zhí)行一個正則表達(dá)式搜索,并返回一個 Boolean 值指示是否找到匹配的模式。RegExp.Global屬性對Test方法沒有影響。如果找到了匹配的模式,Test方法返回True;否則返回False。

例子:

復(fù)制代碼 代碼如下:

Function RegExpTest(patrn, strng)
Dim regEx, retVal            ' 建立變量。
Set regEx = New RegExp         ' 建立正則表達(dá)式。
regEx.Pattern = patrn         ' 設(shè)置模式。
regEx.IgnoreCase = False         ' 設(shè)置是否區(qū)分大小寫。
retVal = regEx.Test(strng)         ' 執(zhí)行搜索測試。
If retVal Then
    RegExpTest = "找到一個或多個匹配。"
Else
    RegExpTest = "未找到匹配。"
End If
End Function
MsgBox(RegExpTest("\d+", "abcd1234"))
MsgBox(RegExpTest("\d+", "abcd"))

Replace 方法替換在正則表達(dá)式查找中找到的文本,例子:

復(fù)制代碼 代碼如下:

Function ReplaceTest(patrn, replStr)
Dim regEx, str1               ' 建立變量。
str1 = "dog 123."
Set regEx = New RegExp               ' 建立正則表達(dá)式。
regEx.Pattern = patrn               ' 設(shè)置模式。
regEx.IgnoreCase = True               ' 設(shè)置是否區(qū)分大小寫。
ReplaceTest = regEx.Replace(str1, replStr)         ' 作替換。
End Function

MsgBox(ReplaceTest("\d+", "cat")) ‘將字符串中的123替換為cat

Execute 方法,則是對指定的字符串執(zhí)行正則表達(dá)式搜索。這里又涉及到Match對像和Matches 集合。Matches 集合就是match的對像集合。Matches 集合中包含若干獨立的 Match 對象,只能使用 RegExp 對象的 Execute 方法來創(chuàng)建之。例子:

復(fù)制代碼 代碼如下:

Function RegExpTest(patrn, strng)
   Dim regEx, Match, Matches   ' 建立變量。
   Set regEx = New RegExp   ' 建立正則表達(dá)式。
   regEx.Pattern = patrn   ' 設(shè)置模式。
   regEx.IgnoreCase = True   ' 設(shè)置是否區(qū)分大小寫。
   regEx.Global = True   ' 設(shè)置全程可用性。
   Set Matches = regEx.Execute(strng)   ' 執(zhí)行搜索。
   For Each Match in Matches      ' 遍歷 Matches 集合。
      RetStr = RetStr & Match.FirstIndex & "。匹配的長度為"&" "
RetStr = RetStr & Match.Length &" "
      RetStr = RetStr & Matches(0) &" "      '值為123
     RetStr = RetStr & Matches(1)&" "      '值為44
    RetStr = RetStr & Match.value&" "    '值為123和44的數(shù)組
     RetStr = RetStr & vbCRLF
   Next
   RegExpTest = RetStr
End Function
MsgBox(RegExpTest("\d+", "123a44"))

相關(guān)文章

最新評論