跳转到内容

Visual Basic/正则表达式测试器

来自维基教科书,开放的书籍,开放的世界

这是一个非常小的程序,它证明了有用的程序并不需要很大。当玩弄在维基教科书中包含的源代码的自动下载和构建的想法时,我需要测试一些正则表达式。我在网上搜索现成的工具,但没有找到真正符合我想要做的事情的工具。Eric Gunnerson 的 C# 工具非常好,但依赖于 .NET 库,对于这样一个小的程序来说,这似乎是一个不必要的巨大开销。所以我花了大约 15 分钟自己制作了一个。它就在这里,它只有最基本错误检查,控件没有整齐排列,并且窗体不可调整大小,或者更确切地说,它是可以调整大小的,但控件不能调整大小。它有很多限制,绝对需要改进,请参见练习

frmRegExpTester.frm

[编辑 | 编辑源代码]

此窗体是程序中唯一的模块。所有困难的工作都由 VBScript 正则表达式库完成。请参见正则表达式

窗体上的控件布局只是我第一个想到的布局。它绝对需要在几个方面改进:可调整大小、制表符顺序等。

对于 RegExp 类别的每个属性和方法都有一个控件,因此您可以行使它的所有功能。每个控件都会通过设置适当的属性并同时调用TestExecute 方法来响应ChangedClick 事件。

 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,因为返回值可以是MatchMatchCollection,具体取决于是否存在一个匹配项或多个匹配项。结果显示在txtExecuteResulttxtTestresult 文本框中。

 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

该程序的全部目的是向用户展示将正则表达式与文本字符串匹配的结果。为此,我们只需创建一个表示MatchMatchCollection 对象的字符串即可。

 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

regexptester.vbp

[编辑 | 编辑源代码]

项目文件非常简单。除了正则表达式库之外,没有其他不寻常的引用。

 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
华夏公益教科书