要正确运行本工具, 需事先要安装vbScript5.6 + VB开发工具.
整个文件可以通过下面的链接下载:
http://www.cnitblog.com/Files/oliver_yin/RegualTool.rar
工程文件: PrjRegualExpressTool.vbp
Type=Exe
Reference=*\G{00020430-0000-0000-C000-000000000046}#2.0#0#C:\WINNT\system32\stdole2.tlb#OLE Automation
Reference=*\G{3F4DACA7-160D-11D2-A8E9-00104B365C9F}#5.5#0#C:\WINNT\system32\vbscript.dll\3#Microsoft VBScript Regular Expressions 5.5
Form=FrmRegTool.frm
Startup="FrmRegTool"
Command32=""
Name="PrjRegualExpressTool"
HelpContextID="0"
CompatibleMode="0"
MajorVer=1
MinorVer=0
RevisionVer=0
AutoIncrementVer=0
ServerSupportFiles=0
VersionCompanyName="gcecn"
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
[MS Transaction Server]
AutoRefresh=1
窗体文件: FrmRegTool.frm
VERSION 5.00
Begin VB.Form FrmRegTool
BorderStyle = 3 'Fixed Dialog
Caption = "正则表达式测试工具"
ClientHeight = 8760
ClientLeft = 45
ClientTop = 330
ClientWidth = 10365
LinkTopic = "Form1"
MaxButton = 0 'False
MinButton = 0 'False
ScaleHeight = 8760
ScaleWidth = 10365
StartUpPosition = 3 'Windows Default
Begin VB.Frame Frame5
Caption = "Replace Text"
BeginProperty Font
Name = "MS Sans Serif"
Size = 12
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 1095
Left = 0
TabIndex = 21
Top = 3360
Width = 10335
Begin VB.TextBox TxtReplace
BeginProperty Font
Name = "MS Sans Serif"
Size = 9.75
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 615
Left = 120
MultiLine = -1 'True
ScrollBars = 3 'Both
TabIndex = 24
Top = 360
Width = 8175
End
Begin VB.CommandButton Command6
Caption = "Cle&ar"
Height = 495
Left = 8520
TabIndex = 23
Top = 360
Width = 735
End
Begin VB.CommandButton Command5
Caption = "Cop&y"
Height = 495
Left = 9480
TabIndex = 22
Top = 360
Width = 735
End
End
Begin VB.Frame Frame4
Caption = "Regex Expression"
BeginProperty Font
Name = "MS Sans Serif"
Size = 12
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 1335
Left = 0
TabIndex = 13
Top = 2040
Width = 5535
Begin VB.CommandButton Command4
Caption = "Cop&y"
Height = 375
Left = 4800
TabIndex = 20
Top = 840
Width = 615
End
Begin VB.CommandButton Command1
Caption = "Cle&ar"
Height = 375
Left = 4800
TabIndex = 19
Top = 360
Width = 615
End
Begin VB.TextBox TxtReg
BeginProperty Font
Name = "MS Sans Serif"
Size = 9.75
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 855
Left = 120
MultiLine = -1 'True
ScrollBars = 2 'Vertical
TabIndex = 14
Text = "FrmRegTool.frx":0000
Top = 360
Width = 4575
End
End
Begin VB.Frame Frame3
Caption = "Text To Match"
BeginProperty Font
Name = "MS Sans Serif"
Size = 12
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 1935
Left = 0
TabIndex = 9
Top = 0
Width = 10335
Begin VB.CommandButton Command2
Caption = "&Paste"
BeginProperty Font
Name = "MS Sans Serif"
Size = 9.75
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 615
Left = 8640
TabIndex = 12
Top = 1200
Width = 1575
End
Begin VB.CommandButton Command3
Caption = "&Clear"
BeginProperty Font
Name = "MS Sans Serif"
Size = 9.75
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 615
Left = 8640
TabIndex = 11
Top = 360
Width = 1575
End
Begin VB.TextBox TxtString
BeginProperty Font
Name = "MS Sans Serif"
Size = 9.75
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 1455
Left = 120
MultiLine = -1 'True
ScrollBars = 3 'Both
TabIndex = 10
Text = "FrmRegTool.frx":0015
Top = 360
Width = 8295
End
End
Begin VB.Frame Frame2
Caption = "Results of match"
BeginProperty Font
Name = "MS Sans Serif"
Size = 12
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 3855
Left = 0
TabIndex = 4
Top = 4440
Width = 10335
Begin VB.CommandButton CmdClear
Caption = "Cl&ear"
BeginProperty Font
Name = "MS Sans Serif"
Size = 9.75
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 375
Left = 1920
TabIndex = 18
Top = 3360
Width = 1695
End
Begin VB.CommandButton CmdCopy
Caption = "&To Clipboard"
BeginProperty Font
Name = "MS Sans Serif"
Size = 9.75
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 375
Left = 4440
TabIndex = 17
Top = 3360
Width = 1695
End
Begin VB.CommandButton CmdSubMatchs
Caption = "&Sub Matches"
BeginProperty Font
Name = "MS Sans Serif"
Size = 9.75
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 615
Left = 8520
TabIndex = 15
Top = 2520
Width = 1575
End
Begin VB.CommandButton CmdMatchs
Caption = "&Match Collection"
BeginProperty Font
Name = "MS Sans Serif"
Size = 9.75
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 615
Left = 8520
TabIndex = 8
Top = 1800
Width = 1575
End
Begin VB.CommandButton CmdReplace
Caption = "&Replace"
BeginProperty Font
Name = "MS Sans Serif"
Size = 9.75
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 615
Left = 8520
TabIndex = 7
Top = 1080
Width = 1575
End
Begin VB.CommandButton CmdTest
Caption = "&ISMatch?"
BeginProperty Font
Name = "MS Sans Serif"
Size = 9.75
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 615
Left = 8520
TabIndex = 6
Top = 360
Width = 1575
End
Begin VB.TextBox TxtOutput
BeginProperty Font
Name = "MS Sans Serif"
Size = 12
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 2895
Left = 120
MultiLine = -1 'True
ScrollBars = 3 'Both
TabIndex = 5
Text = "FrmRegTool.frx":0045
Top = 360
Width = 8175
End
Begin VB.Label Label1
AutoSize = -1 'True
BackStyle = 0 'Transparent
Caption = "本工具后台解析器采用VBScript 5.5."
BeginProperty Font
Name = "MS Sans Serif"
Size = 9.75
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H00FF0000&
Height = 240
Left = 7320
TabIndex = 16
Top = 3600
Width = 2925
WordWrap = -1 'True
End
End
Begin VB.Frame Frame1
Caption = "Regex Options"
BeginProperty Font
Name = "MS Sans Serif"
Size = 12
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 1335
Left = 5640
TabIndex = 0
Top = 2040
Width = 4695
Begin VB.CheckBox ChkMultiLine
Caption = "Multi Line"
BeginProperty Font
Name = "MS Sans Serif"
Size = 12
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 375
Left = 240
TabIndex = 3
Top = 840
Width = 1455
End
Begin VB.CheckBox ChkIgnorecase
Caption = "Ignore Case"
BeginProperty Font
Name = "MS Sans Serif"
Size = 12
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 375
Left = 2400
TabIndex = 2
Top = 360
Width = 1695
End
Begin VB.CheckBox ChkGlobal
Caption = "Global"
BeginProperty Font
Name = "MS Sans Serif"
Size = 12
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 375
Left = 240
TabIndex = 1
Top = 360
Width = 1095
End
End
Begin VB.Label Label2
Caption = "注意:考虑到有可能有特殊字符的匹配, 每个文本框都没有使用Trim进行处理"
ForeColor = &H000000FF&
Height = 255
Left = 120
TabIndex = 25
Top = 8400
Width = 9855
End
End
Attribute VB_Name = "FrmRegTool"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private strTemp As String
Private Sub CmdClear_Click()
TxtOutput.Text = ""
End Sub
Private Sub CmdCopy_Click()
Dim TmpStr As String
'清除剪贴板
Clipboard.Clear
TmpStr = TxtOutput.Text
Clipboard.SetText TmpStr
End Sub
Private Sub CmdMatchs_Click()
'给出所有的match的结果
If Trim(TxtString) <> "" And Trim(TxtReg.Text) <> "" Then
TxtOutput.ForeColor = vbBlack
TxtOutput = RegExpMatchs(TxtReg.Text, TxtString.Text)
End If
End Sub
Private Sub CmdReplace_Click()
'进行替换动作
If Trim(TxtString) <> "" And Trim(TxtReg.Text) <> "" And Trim(TxtReplace.Text) <> "" Then
TxtOutput.ForeColor = vbBlack
TxtOutput = ReplaceTest(TxtReg.Text, TxtReplace.Text, TxtString.Text)
End If
End Sub
Private Sub CmdSubMatchs_Click()
'sub matches
TxtOutput.Text = SubMatchTest(Trim(TxtReg.Text), TxtString.Text)
End Sub
Private Sub CmdTest_Click()
'检查表达式是否有效
If Trim(TxtString) <> "" And Trim(TxtReg.Text) <> "" Then
If RegExpTest(TxtReg.Text, TxtString.Text) Then
TxtOutput.ForeColor = vbRed
TxtOutput.Text = "Match success!"
Else
TxtOutput.Text = "Match fail!"
End If
End If
End Sub
Private Sub Command1_Click()
TxtReg.Text = ""
End Sub
'Private reg As New RegExp
'Private Sub Command1_Click()
''测试正则表达式
' Dim ss, re, rv
'ss = "Is is the cost of of gasoline going up up?." & vbNewLine
'Set re = New RegExp
're.Pattern = "\b([a-z][A-Z]+) \1\b"
're.Global = True
're.IgnoreCase = True
're.MultiLine = True
'rv = re.Replace(ss, "$1")
'
'TxtOutput.Text = rv
'
'
'End Sub
Private Sub Command2_Click()
'从剪贴板Paste
TxtString.Text = Clipboard.GetText
End Sub
Private Sub Command3_Click()
'Clear 文本框
TxtString.Text = ""
End Sub
Private Sub Command4_Click()
Dim TmpStr As String
'清除剪贴板
Clipboard.Clear
TmpStr = TxtReg.Text
Clipboard.SetText TmpStr
End Sub
Private Sub Command5_Click()
Dim TmpStr As String
'清除剪贴板
Clipboard.Clear
TmpStr = TxtReplace.Text
Clipboard.SetText TmpStr
End Sub
Private Sub Command6_Click()
'清空replace 文本
TxtReplace.Text = ""
End Sub
Private Sub Form_Load()
Call Command1_Click
Call Command3_Click
Call Command6_Click
Call CmdClear_Click
End Sub
Function RegExpMatchs(patrn As String, strng As String) As String
'执行有效性测试
Dim regEx As RegExp
Dim Match1 As Match
Dim Matches As MatchCollection
Dim retStr As String ' Create variable.
On Error GoTo ErrorHandle
Screen.MousePointer = 11
Set regEx = New RegExp ' Create a regular expression.
regEx.Pattern = patrn ' Set pattern.
regEx.IgnoreCase = ChkIgnorecase.Value ' Set case insensitivity.
regEx.Global = ChkGlobal.Value ' Set global applicability.
regEx.MultiLine = ChkMultiLine.Value
If regEx.Test(strng) Then
Set Matches = regEx.Execute(strng) ' Execute search.
retStr = retStr & "FirstIndex " & vbTab & "Match Value" & vbCrLf
For Each Match1 In Matches ' Iterate Matches collection.
retStr = retStr & Match1.FirstIndex & vbTab
retStr = retStr & Match1.Value & vbCrLf
Next
RegExpMatchs = retStr
Else
RegExpMatchs = "Match not found"
End If
ExitProcedure:
Screen.MousePointer = 0
Exit Function
ErrorHandle: ' Error-handling routine.
strTemp = "Error # " & Str(Err.Number) & " was generated by " _
& Err.Source & Chr(11) & Err.Description
MsgBox strTemp, vbOKOnly + vbCritical, Me.Caption, Err.HelpFile, Err.HelpContext
RegExpMatchs = "Error!"
Screen.MousePointer = 0
End Function
Function RegExpTest(patrn As String, strng As String) As Boolean
'执行有效性测试
Dim regEx As RegExp
Dim Match1 As Match
Dim Matches As MatchCollection
Dim retStr As String ' Create variable.
On Error GoTo ErrorHandle
Screen.MousePointer = 11
Set regEx = New RegExp ' Create a regular expression.
regEx.Pattern = patrn ' Set pattern.
regEx.IgnoreCase = ChkIgnorecase.Value ' Set case insensitivity.
regEx.Global = ChkGlobal.Value ' Set global applicability.
regEx.MultiLine = ChkMultiLine.Value
RegExpTest = regEx.Test(strng)
ExitProcedure:
Screen.MousePointer = 0
Exit Function
ErrorHandle: ' Error-handling routine.
strTemp = "Error # " & Str(Err.Number) & " was generated by " _
& Err.Source & Chr(11) & Err.Description
MsgBox strTemp, vbOKOnly + vbCritical, Me.Caption, Err.HelpFile, Err.HelpContext
RegExpTest = False
Screen.MousePointer = 0
End Function
Function SubMatchTest(patrn As String, inpStr As String) As String
Dim oRe As RegExp
Dim oMatch As Match
Dim oMatches As MatchCollection
Dim retStr As String
' Dim oSubMatches As SubMatches
Dim i, j As Long
On Error GoTo ErrorHandle
Screen.MousePointer = 11
Set oRe = New RegExp
' Look for an e-mail address (not a perfect RegExp)
'oRe.Pattern = "(\w+)@(\w+)\.(\w+)"
oRe.Pattern = patrn
oRe.IgnoreCase = ChkIgnorecase.Value ' Set case insensitivity.
oRe.Global = ChkGlobal.Value ' Set global applicability.
oRe.MultiLine = ChkMultiLine.Value
If oRe.Test(inpStr) Then
' Get the Matches collection
Set oMatches = oRe.Execute(inpStr)
retStr = "Matches hierarchy :" & vbNewLine
For i = 0 To oMatches.Count - 1
Set oMatch = oMatches(i)
retStr = retStr & "Match " & i & vbTab & oMatches(i) & vbNewLine
For j = 0 To oMatch.SubMatches.Count - 1
retStr = retStr & vbTab & "subMatch " & j & vbTab & oMatch.SubMatches(j) & vbNewLine
Next j
Next i
SubMatchTest = retStr
Else
SubMatchTest = "Match not found"
End If
ExitProcedure:
Screen.MousePointer = 0
Exit Function
ErrorHandle: ' Error-handling routine.
strTemp = "Error # " & Str(Err.Number) & " was generated by " _
& Err.Source & Chr(11) & Err.Description
MsgBox strTemp, vbOKOnly + vbCritical, Me.Caption, Err.HelpFile, Err.HelpContext
SubMatchTest = "Error!"
Screen.MousePointer = 0
End Function
Function ReplaceTest(patrn As String, replStr As String, StrTobeReplace As String) As String
Dim regEx As RegExp
Set regEx = New RegExp ' Create regular expression.
regEx.Pattern = patrn ' Set pattern.
regEx.IgnoreCase = ChkIgnorecase.Value ' Set case insensitivity.
regEx.Global = ChkGlobal.Value ' Set global applicability.
regEx.MultiLine = ChkMultiLine.Value
ReplaceTest = regEx.Replace(StrTobeReplace, replStr) ' Make replacement.
End Function
posted on 2005-08-20 12:15
生活像一团麻 阅读(974)
评论(0) 编辑 收藏 引用 所属分类:
正则表达式