vb 正規表示式2

2021-04-22 14:16:32 字數 1826 閱讀 8864

1.

啟動 microsoft visual basic 6.0。

2.在「檔案」選單上,單擊「新建專案」。

3.在「新建專案」對話方塊中,單擊「standard exe」,然後單擊「確定」。

預設情況下將建立 form1。

4.在「專案」選單上單擊「引用」。

5.雙擊「microsoft vbscript regular expressions 5.5」,然後單擊「確定」。

6.在工具箱中,雙擊「命令按鈕」。

預設情況下,「command1」將新增到窗體中。

7.雙擊「command1」以開啟**視窗。

8.將下面的**貼上到「command1_click」事件處理程式:

msgbox(testregexp("is.", "is1 is2 is3 is4"))
注意這個示例中將對照「is1 is2 is3 is4」字串檢查is.模式。您可以將句點這一特殊字元(.)用作萬用字元,這樣,搜尋模式就能夠多匹配並多顯示乙個字元。如果您在搜尋模式中新增兩個句點,您會看到兩個其他字元。如果您不使用任何句點,您只會看到搜尋模式。

將以下函式新增到「command1_click」事件處理程式後:

function testregexp(mypattern as string, mystring as string)

'create objects.

dim objregexp as regexp

dim objmatch as match

dim colmatches as matchcollection

dim retstr as string

' create a regular expression object.

set objregexp = new regexp

'set the pattern by using the pattern property.

objregexp.pattern = mypattern

' set case insensitivity.

objregexp.ignorecase = true

objregexp.global = true

'test whether the string can be compared.

if (objregexp.test(mystring) = true) then

'get the matches.

set colmatches = objregexp.execute(mystring) ' execute search.

for each objmatch in colmatches ' iterate matches collection.

retstr = retstr & "match found at position "

retstr = retstr & objmatch.firstindex & ". match value is '"

retstr = retstr & objmatch.value & "'." & vbcrlf

next

else

retstr = "string matching failed"

end if

testregexp = retstr

end function

10.在「執行」選單上,單擊「啟動」來執行該應用程式。

11.單擊「command1」。

此時將出現乙個訊息框,該訊息顯示is1 is2 is3 is4字串中的所有is匹配項。

正規表示式 2

例 正規表示式物件 本物件包含正規表示式模式以及表明如何應用模式的標誌。語法 1 re pattern flags 語法 2 re new regexp pattern flags 引數 re 必選項。將要賦值為正規表示式模式的變數名。pattern 必選項。要使用的正規表示式模式。如果使用語法 1...

正規表示式2

本文參照python 核心程式設計第一章正規表示式 1 match函式的使用 import re m re.match foo foo if m is not none print m.group match 如果匹配成功,返回乙個匹配物件,不成功則返回none 如果我們不使用if判斷就使用m.gr...

正規表示式2

4.2字元集合var reg 1a2b3 匹配乙個字元,這個字元必須是 1 a 2 b 3其中一種,如果是就表示滿足,如果不是就不滿足 reg.test a 結果為 true reg.test 3 結果為 true reg.test fg56 乙個符合要求的字元都不存在,結果為 false reg....