ASP 正則表達(dá)式常用的幾種方法(execute、test、replace)
更新時(shí)間:2009年12月14日 03:08:34 作者:
asp下正則表達(dá)式常用的幾種方法,需要的朋友可以參考下。
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í)行一個(gè)正則表達(dá)式搜索,并返回一個(gè) Boolean 值指示是否找到匹配的模式。RegExp.Global屬性對Test方法沒有影響。如果找到了匹配的模式,Test方法返回True;否則返回False。
例子:
測試的時(shí)候,msgbox是vbs的用法,如果是asp文件,需要將msgbox替換為response.write
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 = "找到一個(gè)或多個(gè)匹配。"
Else
RegExpTest = "未找到匹配。"
End If
End Function
MsgBox(RegExpTest("\d+", "abcd1234"))
MsgBox(RegExpTest("\d+", "abcd"))
Replace 方法替換在正則表達(dá)式查找中找到的文本
例子:
vbs代碼
Function ReplaceTest(str,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(str, replStr) ' 作替換。
End Function
MsgBox(ReplaceTest("dog 123","\d+", "cat")) '將字符串中的123替換為cat
Execute 方法,則是對指定的字符串執(zhí)行正則表達(dá)式搜索。這里又涉及到Match對像和Matches 集合。Matches 集合就是match的對像集合。Matches 集合中包含若干獨(dú)立的 Match 對象,只能使用 RegExp 對象的 Execute 方法來創(chuàng)建之。例子:
vbs測試代碼
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"))
如:
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í)行一個(gè)正則表達(dá)式搜索,并返回一個(gè) Boolean 值指示是否找到匹配的模式。RegExp.Global屬性對Test方法沒有影響。如果找到了匹配的模式,Test方法返回True;否則返回False。
例子:
測試的時(shí)候,msgbox是vbs的用法,如果是asp文件,需要將msgbox替換為response.write
復(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 = "找到一個(gè)或多個(gè)匹配。"
Else
RegExpTest = "未找到匹配。"
End If
End Function
MsgBox(RegExpTest("\d+", "abcd1234"))
MsgBox(RegExpTest("\d+", "abcd"))
Replace 方法替換在正則表達(dá)式查找中找到的文本
例子:
vbs代碼
復(fù)制代碼 代碼如下:
Function ReplaceTest(str,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(str, replStr) ' 作替換。
End Function
MsgBox(ReplaceTest("dog 123","\d+", "cat")) '將字符串中的123替換為cat
Execute 方法,則是對指定的字符串執(zhí)行正則表達(dá)式搜索。這里又涉及到Match對像和Matches 集合。Matches 集合就是match的對像集合。Matches 集合中包含若干獨(dú)立的 Match 對象,只能使用 RegExp 對象的 Execute 方法來創(chuàng)建之。例子:
vbs測試代碼
復(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)文章
JS 正則表達(dá)式(學(xué)習(xí)筆記2)匹配網(wǎng)址url參數(shù)
呵呵,剛學(xué)習(xí)正則表達(dá)式,今天幫一個(gè)美女解決了個(gè)問題。感到很高興。先貼下今天都學(xué)了什么吧。不然忘記了。2010-05-0536個(gè)正則表達(dá)式(開發(fā)效率提高80%)
我們在日常的Java開發(fā)中,經(jīng)常需要處理一些字符串,本文主要介紹了36個(gè)常用正則表達(dá)式,感興趣的可以了解一下2021-11-11js正則表達(dá)式之RegExp對象屬性lastIndex,lastMatch,lastParen,lastContext,
js正則表達(dá)式之RegExp對象屬性lastIndex,lastMatch($&),lastParen($+),lastContext,rightContext屬性的講解2012-10-10