Visual Basic/正則表示式測試器
這是一個非常小的程式,它證明了有用的程式並不需要很大。當玩弄在華夏公益教科書中包含的原始碼的自動下載和構建的想法時,我需要測試一些正則表示式。我在網上搜索現成的工具,但沒有找到真正符合我想要做的事情的工具。Eric Gunnerson 的 C# 工具非常好,但依賴於 .NET 庫,對於這樣一個小的程式來說,這似乎是一個不必要的巨大開銷。所以我花了大約 15 分鐘自己製作了一個。它就在這裡,它只有最基本錯誤檢查,控制元件沒有整齊排列,並且窗體不可調整大小,或者更確切地說,它是可以調整大小的,但控制元件不能調整大小。它有很多限制,絕對需要改進,請參見練習。
此窗體是程式中唯一的模組。所有困難的工作都由 VBScript 正則表示式庫完成。請參見正則表示式。
窗體上的控制元件佈局只是我第一個想到的佈局。它絕對需要在幾個方面改進:可調整大小、製表符順序等。
對於 RegExp 類別的每個屬性和方法都有一個控制元件,因此您可以行使它的所有功能。每個控制元件都會透過設定適當的屬性並同時呼叫Test 和Execute 方法來響應Changed 和Click 事件。
VERSION 5.00
Begin VB.Form frmRegExpTester
Caption = "Form1"
ClientHeight = 11700
ClientLeft = 60
ClientTop = 360
ClientWidth = 11010
BeginProperty Font
Name = "System"
Size = 9.75
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
LinkTopic = "Form1"
ScaleHeight = 11700
ScaleWidth = 11010
StartUpPosition = 3 'Windows Default
Begin VB.TextBox txtTestresult
BeginProperty Font
Name = "Fixedsys"
Size = 9
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 495
Left = 1320
TabIndex = 10
Text = "Text1"
Top = 2880
Width = 4095
End
Begin VB.TextBox txtExecuteresult
BeginProperty Font
Name = "Fixedsys"
Size = 9
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 9135
Left = 6840
MultiLine = -1 'True
TabIndex = 9
Text = "frmRegExpTester.frx":0000
Top = 2520
Width = 4095
End
Begin VB.TextBox txtReplace
BeginProperty Font
Name = "Fixedsys"
Size = 9
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 615
Left = 960
MultiLine = -1 'True
TabIndex = 5
Text = "frmRegExpTester.frx":0006
Top = 1440
Width = 8295
End
Begin VB.CheckBox chkMultiLine
Caption = "Multiline"
BeginProperty Font
Name = "MS Sans Serif"
Size = 8.25
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 495
Left = 3360
TabIndex = 4
Top = 840
Value = 1 'Checked
Width = 1575
End
Begin VB.CheckBox chkIgnoreCase
Caption = "Ignore Case"
BeginProperty Font
Name = "MS Sans Serif"
Size = 8.25
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 375
Left = 1800
TabIndex = 3
Top = 960
Value = 1 'Checked
Width = 1575
End
Begin VB.CheckBox chkGlobal
Caption = "Global"
BeginProperty Font
Name = "MS Sans Serif"
Size = 8.25
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 375
Left = 240
TabIndex = 2
Top = 960
Value = 1 'Checked
Width = 1455
End
Begin VB.TextBox txtPattern
BeginProperty Font
Name = "Fixedsys"
Size = 9
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 615
Left = 840
MultiLine = -1 'True
TabIndex = 1
Text = "frmRegExpTester.frx":000C
Top = 120
Width = 8535
End
Begin VB.TextBox txtSource
BeginProperty Font
Name = "Fixedsys"
Size = 9
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 6735
Left = 0
MultiLine = -1 'True
TabIndex = 0
Text = "frmRegExpTester.frx":0022
Top = 4920
Width = 5655
End
Begin VB.Label Label3
Caption = "Test status"
BeginProperty Font
Name = "MS Sans Serif"
Size = 8.25
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 255
Left = 0
TabIndex = 11
Top = 3000
Width = 1215
End
Begin VB.Label Label2
Caption = "Execute status"
BeginProperty Font
Name = "MS Sans Serif"
Size = 8.25
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 255
Left = 5520
TabIndex = 8
Top = 2640
Width = 1215
End
Begin VB.Label Label1
Caption = "Replacement"
BeginProperty Font
Name = "MS Sans Serif"
Size = 8.25
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 375
Index = 1
Left = 0
TabIndex = 7
Top = 1560
Width = 735
End
Begin VB.Label Label1
Caption = "Pattern"
BeginProperty Font
Name = "MS Sans Serif"
Size = 8.25
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 375
Index = 0
Left = 0
TabIndex = 6
Top = 240
Width = 735
End
End
Attribute VB_Name = "frmRegExpTester"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
RegExp 例項可用於此模組中的所有程式碼,儘管這並非嚴格必要,因為我們可以每次建立一個新的例項。
Private moRe As RegExp
每個Boolean 屬性都由一個CheckBox 控制元件表示。每個控制元件的點選事件只是設定適當的屬性值並透過xExecute 本地子例程呼叫Execute 方法。
Private Sub chkGlobal_Click() moRe.Global = chkGlobal = vbChecked xExecute End Sub Private Sub chkIgnoreCase_Click() moRe.IgnoreCase = chkIgnoreCase = vbChecked xExecute End Sub Private Sub chkMultiLine_Click() moRe.MultiLine = chkMultiLine = vbChecked xExecute End Sub
當建立窗體時,會呼叫Initialize 事件處理程式。這將建立 RegExp 例項並將屬性設定為與控制元件的值相對應。
Private Sub Form_Initialize()
Set moRe = New RegExp
With moRe
.Global = chkGlobal = vbChecked
.IgnoreCase = chkIgnoreCase = vbChecked
.MultiLine = chkMultiLine = vbChecked
.Pattern = txtPattern
End With
End Sub
當用戶更改正則表示式 或源文字時,透過呼叫xExecute 更新輸出。
Private Sub txtPattern_Change() xExecute End Sub Private Sub txtSource_Change() xExecute End Sub
這是程式的核心。它所做的就是呼叫Execute 方法,然後構造一個字串來表示結果。如果模式 無效,則Execute 方法將引發錯誤。這會被捕獲,並且錯誤描述將用於代替結果。結果物件被宣告為Object,因為返回值可以是Match 或MatchCollection,具體取決於是否存在一個匹配項或多個匹配項。結果顯示在txtExecuteResult 和txtTestresult 文字框中。
Private Sub xExecute()
moRe.Pattern = txtPattern
Dim o As Object
On Error Resume Next
Set o = moRe.Execute(txtSource)
If Err Then
txtExecuteresult = Err.Number & ", " & Err.Description
Else
On Error GoTo 0
If TypeOf o Is Match Then
txtExecuteresult = xMatchToString(o)
ElseIf TypeOf o Is MatchCollection Then
txtExecuteresult = xMatchCollectionToString(o)
End If
End If
On Error GoTo 0
On Error Resume Next
txtTestresult = moRe.Test(txtSource)
If Err Then
txtTestresult = Err.Number & ", " & Err.Description
End If
On Error GoTo 0
End Sub
該程式的全部目的是向用戶展示將正則表示式與文字字串匹配的結果。為此,我們只需建立一個表示Match 或MatchCollection 物件的字串即可。
Private Function xMatchCollectionToString(ByRef roMatchCollection As MatchCollection) As String
Dim lX As Long
With roMatchCollection
xMatchCollectionToString = ".Count:" & .Count & vbCrLf
For lX = 0 To .Count - 1
xMatchCollectionToString = xMatchCollectionToString _
& "Item " & lX & vbCrLf _
& xMatchToString(.Item(lX))
Next lX
End With
End Function
Private Function xMatchToString(ByRef roMatch As Match) As String
With roMatch
xMatchToString = ".FirstIndex:" & .FirstIndex & vbCrLf _
& " .Length:" & .Length & vbCrLf _
& " .Value:" & .Value & vbCrLf _
& ".SubMatches:" & vbCrLf _
& xSubMatchesToString(.SubMatches) & vbCrLf
End With
End Function
Private Function xSubMatchesToString(ByRef roSubMatches As SubMatches) As String
Dim lX As Long
With roSubMatches
xSubMatchesToString = ".Count:" & .Count & vbCrLf
For lX = 0 To .Count - 1
xSubMatchesToString = xSubMatchesToString _
& "Item " & lX & vbCrLf _
& """" & .Item(lX) & """"
Next lX
End With
End Function
專案檔案非常簡單。除了正則表示式庫之外,沒有其他不尋常的引用。
Type=Exe
Reference=*\G{00020430-0000-0000-C000-000000000046}#2.0#0#..\..\..\..\..\..\WINNT\System32\stdole2.tlb#OLE Automation
Reference=*\G{3F4DACA7-160D-11D2-A8E9-00104B365C9F}#5.5#0#..\..\..\..\..\..\WINNT\System32\vbscript.dll\3#Microsoft VBScript Regular Expressions 5.5
Form=frmRegExpTester.frm
Startup="frmRegExpTester"
HelpFile=""
Title="prjGet"
Command32="C:\users\kjw\homepage\website\vb\wikirepository\Jarithmetic_Round_Two_Implementation.html"
Name="prjRegExpTester"
HelpContextID="0"
CompatibleMode="0"
MajorVer=1
MinorVer=0
RevisionVer=0
AutoIncrementVer=0
ServerSupportFiles=0
VersionCompanyName=""
CompilationType=0
OptimizationType=0
FavorPentiumPro(tm)=0
CodeViewDebugInfo=0
NoAliasing=0
BoundsCheck=0
OverflowCheck=0
FlPointCheck=0
FDIVCheck=0
UnroundedFP=0
StartMode=0
Unattended=0
Retained=0
ThreadPerObject=0
MaxNumberOfThreads=1
練習
- 執行程式,看看你是否能使它崩潰。
- 整理使用者介面,新增加速鍵以幫助導航,修復製表符順序。
- 想一想可能值得新增的功能,並新增它。
| 上一個:案例研究 | 目錄 | 下一個:JArithmetic |