最近学习一下正则,顺便把书里的例子抄袭一下。^ ^ ...
Imports
System.IO
Imports System.Text
Imports System.Text.RegularExpressions
Public Class Form1 Class Form1
Inherits System.Windows.Forms.Form
Windows 窗体设计器生成的代码#Region " Windows 窗体设计器生成的代码 "
Public Sub New()Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose()Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents cmdTestRegex As System.Windows.Forms.Button
Friend WithEvents cmdReplace As System.Windows.Forms.Button
Friend WithEvents cmdMatches As System.Windows.Forms.Button
Friend WithEvents cmdSplit As System.Windows.Forms.Button
Friend WithEvents cmdOpenRegex As System.Windows.Forms.Button
Friend WithEvents cmdSaveRegex As System.Windows.Forms.Button
Friend WithEvents grpOptions As System.Windows.Forms.GroupBox
Friend WithEvents chkSingleLine As System.Windows.Forms.CheckBox
Friend WithEvents chkRightToLeft As System.Windows.Forms.CheckBox
Friend WithEvents chkMultiLine As System.Windows.Forms.CheckBox
Friend WithEvents chkIgnorePatternWhiteSpace As System.Windows.Forms.CheckBox
Friend WithEvents chkIgnoreCase As System.Windows.Forms.CheckBox
Friend WithEvents chkExplicitCapture As System.Windows.Forms.CheckBox
Friend WithEvents chkECMAScript As System.Windows.Forms.CheckBox
Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog
Friend WithEvents txtResults As System.Windows.Forms.TextBox
Friend WithEvents txtReplacementText As System.Windows.Forms.TextBox
Friend WithEvents txtRegex As System.Windows.Forms.TextBox
Friend WithEvents txtInputText As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label
Me.txtRegex = New System.Windows.Forms.TextBox
Me.grpOptions = New System.Windows.Forms.GroupBox
Me.chkIgnorePatternWhiteSpace = New System.Windows.Forms.CheckBox
Me.chkECMAScript = New System.Windows.Forms.CheckBox
Me.chkExplicitCapture = New System.Windows.Forms.CheckBox
Me.chkIgnoreCase = New System.Windows.Forms.CheckBox
Me.chkMultiLine = New System.Windows.Forms.CheckBox
Me.chkRightToLeft = New System.Windows.Forms.CheckBox
Me.chkSingleLine = New System.Windows.Forms.CheckBox
Me.cmdOpenRegex = New System.Windows.Forms.Button
Me.cmdSaveRegex = New System.Windows.Forms.Button
Me.Label2 = New System.Windows.Forms.Label
Me.txtInputText = New System.Windows.Forms.TextBox
Me.txtReplacementText = New System.Windows.Forms.TextBox
Me.Label3 = New System.Windows.Forms.Label
Me.txtResults = New System.Windows.Forms.TextBox
Me.Label4 = New System.Windows.Forms.Label
Me.cmdReplace = New System.Windows.Forms.Button
Me.cmdTestRegex = New System.Windows.Forms.Button
Me.cmdMatches = New System.Windows.Forms.Button
Me.cmdSplit = New System.Windows.Forms.Button
Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog
Me.grpOptions.SuspendLayout()
Me.SuspendLayout()
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(8, 8)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(144, 23)
Me.Label1.TabIndex = 0
Me.Label1.Text = "Regular Expression"
Me.Label1.TextAlign = System.Drawing.ContentAlignment.BottomLeft
'
'txtRegex
'
Me.txtRegex.Location = New System.Drawing.Point(8, 32)
Me.txtRegex.Multiline = True
Me.txtRegex.Name = "txtRegex"
Me.txtRegex.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
Me.txtRegex.Size = New System.Drawing.Size(472, 48)
Me.txtRegex.TabIndex = 1
Me.txtRegex.Text = ""
'
'grpOptions
'
Me.grpOptions.Controls.Add(Me.chkIgnorePatternWhiteSpace)
Me.grpOptions.Controls.Add(Me.chkECMAScript)
Me.grpOptions.Controls.Add(Me.chkExplicitCapture)
Me.grpOptions.Controls.Add(Me.chkIgnoreCase)
Me.grpOptions.Controls.Add(Me.chkMultiLine)
Me.grpOptions.Controls.Add(Me.chkRightToLeft)
Me.grpOptions.Controls.Add(Me.chkSingleLine)
Me.grpOptions.Location = New System.Drawing.Point(8, 80)
Me.grpOptions.Name = "grpOptions"
Me.grpOptions.Size = New System.Drawing.Size(304, 112)
Me.grpOptions.TabIndex = 2
Me.grpOptions.TabStop = False
Me.grpOptions.Text = "Regex Options"
'
'chkIgnorePatternWhiteSpace
'
Me.chkIgnorePatternWhiteSpace.Location = New System.Drawing.Point(8, 72)
Me.chkIgnorePatternWhiteSpace.Name = "chkIgnorePatternWhiteSpace"
Me.chkIgnorePatternWhiteSpace.Size = New System.Drawing.Size(184, 24)
Me.chkIgnorePatternWhiteSpace.TabIndex = 6
Me.chkIgnorePatternWhiteSpace.Text = "IgnorePatternWhiteSpace"
'
'chkECMAScript
'
Me.chkECMAScript.Location = New System.Drawing.Point(200, 44)
Me.chkECMAScript.Name = "chkECMAScript"
Me.chkECMAScript.Size = New System.Drawing.Size(88, 24)
Me.chkECMAScript.TabIndex = 5
Me.chkECMAScript.Text = "ECMAScript"
'
'chkExplicitCapture
'
Me.chkExplicitCapture.Location = New System.Drawing.Point(96, 44)
Me.chkExplicitCapture.Name = "chkExplicitCapture"
Me.chkExplicitCapture.TabIndex = 4
Me.chkExplicitCapture.Text = "ExplicitCapture"
'
'chkIgnoreCase
'
Me.chkIgnoreCase.Location = New System.Drawing.Point(8, 44)
Me.chkIgnoreCase.Name = "chkIgnoreCase"
Me.chkIgnoreCase.TabIndex = 3
Me.chkIgnoreCase.Text = "IgnoreCase"
'
'chkMultiLine
'
Me.chkMultiLine.Location = New System.Drawing.Point(200, 16)
Me.chkMultiLine.Name = "chkMultiLine"
Me.chkMultiLine.Size = New System.Drawing.Size(88, 24)
Me.chkMultiLine.TabIndex = 2
Me.chkMultiLine.Text = "MultiLine"
'
'chkRightToLeft
'
Me.chkRightToLeft.Location = New System.Drawing.Point(96, 16)
Me.chkRightToLeft.Name = "chkRightToLeft"
Me.chkRightToLeft.TabIndex = 1
Me.chkRightToLeft.Text = "RightToLeft"
'
'chkSingleLine
'
Me.chkSingleLine.Location = New System.Drawing.Point(8, 16)
Me.chkSingleLine.Name = "chkSingleLine"
Me.chkSingleLine.TabIndex = 0
Me.chkSingleLine.Text = "SingleLine"
'
'cmdOpenRegex
'
Me.cmdOpenRegex.Location = New System.Drawing.Point(320, 88)
Me.cmdOpenRegex.Name = "cmdOpenRegex"
Me.cmdOpenRegex.TabIndex = 3
Me.cmdOpenRegex.Text = "OpenRegex"
'
'cmdSaveRegex
'
Me.cmdSaveRegex.Location = New System.Drawing.Point(408, 88)
Me.cmdSaveRegex.Name = "cmdSaveRegex"
Me.cmdSaveRegex.TabIndex = 4
Me.cmdSaveRegex.Text = "SaveRegex"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(8, 200)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(112, 23)
Me.Label2.TabIndex = 5
Me.Label2.Text = "Text to Match on"
Me.Label2.TextAlign = System.Drawing.ContentAlignment.BottomLeft
'
'txtInputText
'
Me.txtInputText.Location = New System.Drawing.Point(9, 224)
Me.txtInputText.Multiline = True
Me.txtInputText.Name = "txtInputText"
Me.txtInputText.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
Me.txtInputText.Size = New System.Drawing.Size(471, 48)
Me.txtInputText.TabIndex = 6
Me.txtInputText.Text = ""
'
'txtReplacementText
'
Me.txtReplacementText.Location = New System.Drawing.Point(10, 304)
Me.txtReplacementText.Multiline = True
Me.txtReplacementText.Name = "txtReplacementText"
Me.txtReplacementText.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
Me.txtReplacementText.Size = New System.Drawing.Size(470, 32)
Me.txtReplacementText.TabIndex = 8
Me.txtReplacementText.Text = ""
'
'Label3
'
Me.Label3.Location = New System.Drawing.Point(9, 280)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(111, 23)
Me.Label3.TabIndex = 7
Me.Label3.Text = "ReplacementText"
Me.Label3.TextAlign = System.Drawing.ContentAlignment.BottomLeft
'
'txtResults
'
Me.txtResults.Location = New System.Drawing.Point(10, 360)
Me.txtResults.Multiline = True
Me.txtResults.Name = "txtResults"
Me.txtResults.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
Me.txtResults.Size = New System.Drawing.Size(470, 64)
Me.txtResults.TabIndex = 10
Me.txtResults.Text = ""
'
'Label4
'
Me.Label4.Location = New System.Drawing.Point(9, 336)
Me.Label4.Name = "Label4"
Me.Label4.TabIndex = 9
Me.Label4.Text = "Results of Match"
Me.Label4.TextAlign = System.Drawing.ContentAlignment.BottomLeft
'
'cmdReplace
'
Me.cmdReplace.Location = New System.Drawing.Point(104, 432)
Me.cmdReplace.Name = "cmdReplace"
Me.cmdReplace.TabIndex = 12
Me.cmdReplace.Text = "Replace()"
'
'cmdTestRegex
'
Me.cmdTestRegex.Location = New System.Drawing.Point(16, 432)
Me.cmdTestRegex.Name = "cmdTestRegex"
Me.cmdTestRegex.TabIndex = 11
Me.cmdTestRegex.Text = "IsMatch()"
'
'cmdMatches
'
Me.cmdMatches.Location = New System.Drawing.Point(280, 432)
Me.cmdMatches.Name = "cmdMatches"
Me.cmdMatches.TabIndex = 14
Me.cmdMatches.Text = "Matches()"
'
'cmdSplit
'
Me.cmdSplit.Location = New System.Drawing.Point(192, 432)
Me.cmdSplit.Name = "cmdSplit"
Me.cmdSplit.TabIndex = 13
Me.cmdSplit.Text = "Split()"
'
'OpenFileDialog1
'
'
'SaveFileDialog1
'
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 15)
Me.ClientSize = New System.Drawing.Size(492, 466)
Me.Controls.Add(Me.cmdMatches)
Me.Controls.Add(Me.cmdSplit)
Me.Controls.Add(Me.cmdReplace)
Me.Controls.Add(Me.cmdTestRegex)
Me.Controls.Add(Me.txtResults)
Me.Controls.Add(Me.Label4)
Me.Controls.Add(Me.txtReplacementText)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.txtInputText)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.cmdSaveRegex)
Me.Controls.Add(Me.cmdOpenRegex)
Me.Controls.Add(Me.grpOptions)
Me.Controls.Add(Me.txtRegex)
Me.Controls.Add(Me.Label1)
Me.Font = New System.Drawing.Font("Tahoma", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.MaximizeBox = False
Me.MaximumSize = New System.Drawing.Size(500, 500)
Me.MinimizeBox = False
Me.MinimumSize = New System.Drawing.Size(500, 500)
Me.Name = "Form1"
Me.Text = "Regular Expression Tester"
Me.grpOptions.ResumeLayout(False)
Me.ResumeLayout(False)
End Sub
#End Region
'保存正则表达式
Private Sub cmdSaveRegex_Click()Sub cmdSaveRegex_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSaveRegex.Click
Me.SaveFileDialog1.ShowDialog()
End Sub
'保存文件
Private Sub SaveFileDialog1_FileOk()Sub SaveFileDialog1_FileOk(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk
Dim filePath As String = Me.SaveFileDialog1.FileName
Dim streamWriterRegex As StreamWriter = File.CreateText(filePath)
streamWriterRegex.Write(Me.txtRegex.Text)
streamWriterRegex.Close()
End Sub
'打开正则表达式
Private Sub cmdOpenRegex_Click()Sub cmdOpenRegex_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOpenRegex.Click
Me.OpenFileDialog1.ShowDialog()
End Sub
'打开文件
Private Sub OpenFileDialog1_FileOk()Sub OpenFileDialog1_FileOk(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
Dim filePath As String = Me.OpenFileDialog1.FileName
Dim streamReaderRegex As StreamReader = File.OpenText(filePath)
Me.txtRegex.Text = streamReaderRegex.ReadToEnd
streamReaderRegex.Close()
End Sub
'IsMatch 方法
Private Sub cmdTestRegex_Click()Sub cmdTestRegex_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTestRegex.Click
Try
'获得验证选项
Dim selectRegexOptions As RegexOptions = GetSelectedRegexOptions()
Dim testRegex As New Regex(Me.txtRegex.Text, selectRegexOptions)
If testRegex.IsMatch(Me.txtInputText.Text) Then
Me.txtResults.ForeColor = Drawing.Color.Red
Me.txtResults.Text = "Match Found"
Else
Me.txtResults.ForeColor = Drawing.Color.Black
Me.txtResults.Text = "No Match Found"
End If
Catch ex As ArgumentException '参数无效引发的异常
Me.txtResults.ForeColor = Drawing.Color.Red
Me.txtResults.Text = "There was an error in your regular expression when " _
& ControlChars.CrLf & ex.Message
End Try
End Sub
'Replace 方法
Private Sub cmdReplace_Click()Sub cmdReplace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdReplace.Click
Try
Dim selectRegexOptions As RegexOptions = GetSelectedRegexOptions()
Dim replaceRegex As New Regex(Me.txtRegex.Text, selectRegexOptions)
Me.txtResults.ForeColor = Drawing.Color.Black
Me.txtResults.Text = replaceRegex.Replace(Me.txtInputText.Text, Me.txtReplacementText.Text)
Catch ex As ArgumentException
Me.txtResults.ForeColor = Drawing.Color.Red
Me.txtResults.Text = "There was an error in your regular expression when " _
& ControlChars.CrLf & ex.Message
End Try
End Sub
'Split 方法
Private Sub cmdSplit_Click()Sub cmdSplit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSplit.Click
Try
Dim selectRegexOptions As RegexOptions = GetSelectedRegexOptions()
Dim splitRegex As New Regex(Me.txtRegex.Text, selectRegexOptions)
Dim splitResults() As String
Me.txtResults.ForeColor = Drawing.Color.Black
splitResults = splitRegex.Split(Me.txtInputText.Text)
Dim strElement As String
Dim strBuilder As New StringBuilder
For Each strElement In splitResults
strBuilder.Append(strElement & ControlChars.CrLf)
Next
Me.txtResults.Text = strBuilder.ToString()
Catch ex As ArgumentException
Me.txtResults.ForeColor = Drawing.Color.Red
Me.txtResults.Text = "There was an error in your regular expression when " _
& ControlChars.CrLf & ex.Message
End Try
End Sub
'Matches 方法
Private Sub cmdMatches_Click()Sub cmdMatches_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdMatches.Click
Try
Dim selectRegexOptions As RegexOptions = GetSelectedRegexOptions()
Dim matchesRegex As New Regex(Me.txtRegex.Text, selectRegexOptions)
Dim matchesFound As MatchCollection
Dim matchMade As Match
Dim strBuilder As New StringBuilder
matchesFound = matchesRegex.Matches(Me.txtInputText.Text)
Dim nextMatch As String = "----- Next Match -----" & ControlChars.CrLf
For Each matchMade In matchesFound
strBuilder.Append(matchMade.Value & ControlChars.CrLf)
strBuilder.Append(nextMatch)
Next
Me.txtResults.ForeColor = Drawing.Color.Black
Me.txtResults.Text = strBuilder.ToString()
Catch ex As Exception
Me.txtResults.ForeColor = Drawing.Color.Red
Me.txtResults.Text = "There was an error in your regular expression when " _
& ControlChars.CrLf & ex.Message
End Try
End Sub
'按选择项进行验证操作的方法
Private Function GetSelectedRegexOptions()Function GetSelectedRegexOptions() As RegexOptions
Dim selectRegexOptions As RegexOptions
If Me.chkECMAScript.Checked Then
selectRegexOptions = selectRegexOptions Or RegexOptions.ECMAScript
End If
If Me.chkExplicitCapture.Checked Then
selectRegexOptions = selectRegexOptions Or RegexOptions.ExplicitCapture
End If
If Me.chkIgnoreCase.Checked Then
selectRegexOptions = selectRegexOptions Or RegexOptions.IgnoreCase
End If
If Me.chkIgnorePatternWhiteSpace.Checked Then
selectRegexOptions = selectRegexOptions Or RegexOptions.IgnorePatternWhitespace
End If
If Me.chkMultiLine.Checked Then
selectRegexOptions = selectRegexOptions Or RegexOptions.Multiline
End If
If Me.chkRightToLeft.Checked Then
selectRegexOptions = selectRegexOptions Or RegexOptions.RightToLeft
End If
If Me.chkSingleLine.Checked Then
selectRegexOptions = selectRegexOptions Or RegexOptions.Singleline
End If
Return selectRegexOptions
End Function
End Class
Imports System.Text
Imports System.Text.RegularExpressions
Public Class Form1 Class Form1
Inherits System.Windows.Forms.Form
Windows 窗体设计器生成的代码#Region " Windows 窗体设计器生成的代码 "
Public Sub New()Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose()Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents cmdTestRegex As System.Windows.Forms.Button
Friend WithEvents cmdReplace As System.Windows.Forms.Button
Friend WithEvents cmdMatches As System.Windows.Forms.Button
Friend WithEvents cmdSplit As System.Windows.Forms.Button
Friend WithEvents cmdOpenRegex As System.Windows.Forms.Button
Friend WithEvents cmdSaveRegex As System.Windows.Forms.Button
Friend WithEvents grpOptions As System.Windows.Forms.GroupBox
Friend WithEvents chkSingleLine As System.Windows.Forms.CheckBox
Friend WithEvents chkRightToLeft As System.Windows.Forms.CheckBox
Friend WithEvents chkMultiLine As System.Windows.Forms.CheckBox
Friend WithEvents chkIgnorePatternWhiteSpace As System.Windows.Forms.CheckBox
Friend WithEvents chkIgnoreCase As System.Windows.Forms.CheckBox
Friend WithEvents chkExplicitCapture As System.Windows.Forms.CheckBox
Friend WithEvents chkECMAScript As System.Windows.Forms.CheckBox
Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog
Friend WithEvents txtResults As System.Windows.Forms.TextBox
Friend WithEvents txtReplacementText As System.Windows.Forms.TextBox
Friend WithEvents txtRegex As System.Windows.Forms.TextBox
Friend WithEvents txtInputText As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label
Me.txtRegex = New System.Windows.Forms.TextBox
Me.grpOptions = New System.Windows.Forms.GroupBox
Me.chkIgnorePatternWhiteSpace = New System.Windows.Forms.CheckBox
Me.chkECMAScript = New System.Windows.Forms.CheckBox
Me.chkExplicitCapture = New System.Windows.Forms.CheckBox
Me.chkIgnoreCase = New System.Windows.Forms.CheckBox
Me.chkMultiLine = New System.Windows.Forms.CheckBox
Me.chkRightToLeft = New System.Windows.Forms.CheckBox
Me.chkSingleLine = New System.Windows.Forms.CheckBox
Me.cmdOpenRegex = New System.Windows.Forms.Button
Me.cmdSaveRegex = New System.Windows.Forms.Button
Me.Label2 = New System.Windows.Forms.Label
Me.txtInputText = New System.Windows.Forms.TextBox
Me.txtReplacementText = New System.Windows.Forms.TextBox
Me.Label3 = New System.Windows.Forms.Label
Me.txtResults = New System.Windows.Forms.TextBox
Me.Label4 = New System.Windows.Forms.Label
Me.cmdReplace = New System.Windows.Forms.Button
Me.cmdTestRegex = New System.Windows.Forms.Button
Me.cmdMatches = New System.Windows.Forms.Button
Me.cmdSplit = New System.Windows.Forms.Button
Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog
Me.grpOptions.SuspendLayout()
Me.SuspendLayout()
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(8, 8)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(144, 23)
Me.Label1.TabIndex = 0
Me.Label1.Text = "Regular Expression"
Me.Label1.TextAlign = System.Drawing.ContentAlignment.BottomLeft
'
'txtRegex
'
Me.txtRegex.Location = New System.Drawing.Point(8, 32)
Me.txtRegex.Multiline = True
Me.txtRegex.Name = "txtRegex"
Me.txtRegex.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
Me.txtRegex.Size = New System.Drawing.Size(472, 48)
Me.txtRegex.TabIndex = 1
Me.txtRegex.Text = ""
'
'grpOptions
'
Me.grpOptions.Controls.Add(Me.chkIgnorePatternWhiteSpace)
Me.grpOptions.Controls.Add(Me.chkECMAScript)
Me.grpOptions.Controls.Add(Me.chkExplicitCapture)
Me.grpOptions.Controls.Add(Me.chkIgnoreCase)
Me.grpOptions.Controls.Add(Me.chkMultiLine)
Me.grpOptions.Controls.Add(Me.chkRightToLeft)
Me.grpOptions.Controls.Add(Me.chkSingleLine)
Me.grpOptions.Location = New System.Drawing.Point(8, 80)
Me.grpOptions.Name = "grpOptions"
Me.grpOptions.Size = New System.Drawing.Size(304, 112)
Me.grpOptions.TabIndex = 2
Me.grpOptions.TabStop = False
Me.grpOptions.Text = "Regex Options"
'
'chkIgnorePatternWhiteSpace
'
Me.chkIgnorePatternWhiteSpace.Location = New System.Drawing.Point(8, 72)
Me.chkIgnorePatternWhiteSpace.Name = "chkIgnorePatternWhiteSpace"
Me.chkIgnorePatternWhiteSpace.Size = New System.Drawing.Size(184, 24)
Me.chkIgnorePatternWhiteSpace.TabIndex = 6
Me.chkIgnorePatternWhiteSpace.Text = "IgnorePatternWhiteSpace"
'
'chkECMAScript
'
Me.chkECMAScript.Location = New System.Drawing.Point(200, 44)
Me.chkECMAScript.Name = "chkECMAScript"
Me.chkECMAScript.Size = New System.Drawing.Size(88, 24)
Me.chkECMAScript.TabIndex = 5
Me.chkECMAScript.Text = "ECMAScript"
'
'chkExplicitCapture
'
Me.chkExplicitCapture.Location = New System.Drawing.Point(96, 44)
Me.chkExplicitCapture.Name = "chkExplicitCapture"
Me.chkExplicitCapture.TabIndex = 4
Me.chkExplicitCapture.Text = "ExplicitCapture"
'
'chkIgnoreCase
'
Me.chkIgnoreCase.Location = New System.Drawing.Point(8, 44)
Me.chkIgnoreCase.Name = "chkIgnoreCase"
Me.chkIgnoreCase.TabIndex = 3
Me.chkIgnoreCase.Text = "IgnoreCase"
'
'chkMultiLine
'
Me.chkMultiLine.Location = New System.Drawing.Point(200, 16)
Me.chkMultiLine.Name = "chkMultiLine"
Me.chkMultiLine.Size = New System.Drawing.Size(88, 24)
Me.chkMultiLine.TabIndex = 2
Me.chkMultiLine.Text = "MultiLine"
'
'chkRightToLeft
'
Me.chkRightToLeft.Location = New System.Drawing.Point(96, 16)
Me.chkRightToLeft.Name = "chkRightToLeft"
Me.chkRightToLeft.TabIndex = 1
Me.chkRightToLeft.Text = "RightToLeft"
'
'chkSingleLine
'
Me.chkSingleLine.Location = New System.Drawing.Point(8, 16)
Me.chkSingleLine.Name = "chkSingleLine"
Me.chkSingleLine.TabIndex = 0
Me.chkSingleLine.Text = "SingleLine"
'
'cmdOpenRegex
'
Me.cmdOpenRegex.Location = New System.Drawing.Point(320, 88)
Me.cmdOpenRegex.Name = "cmdOpenRegex"
Me.cmdOpenRegex.TabIndex = 3
Me.cmdOpenRegex.Text = "OpenRegex"
'
'cmdSaveRegex
'
Me.cmdSaveRegex.Location = New System.Drawing.Point(408, 88)
Me.cmdSaveRegex.Name = "cmdSaveRegex"
Me.cmdSaveRegex.TabIndex = 4
Me.cmdSaveRegex.Text = "SaveRegex"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(8, 200)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(112, 23)
Me.Label2.TabIndex = 5
Me.Label2.Text = "Text to Match on"
Me.Label2.TextAlign = System.Drawing.ContentAlignment.BottomLeft
'
'txtInputText
'
Me.txtInputText.Location = New System.Drawing.Point(9, 224)
Me.txtInputText.Multiline = True
Me.txtInputText.Name = "txtInputText"
Me.txtInputText.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
Me.txtInputText.Size = New System.Drawing.Size(471, 48)
Me.txtInputText.TabIndex = 6
Me.txtInputText.Text = ""
'
'txtReplacementText
'
Me.txtReplacementText.Location = New System.Drawing.Point(10, 304)
Me.txtReplacementText.Multiline = True
Me.txtReplacementText.Name = "txtReplacementText"
Me.txtReplacementText.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
Me.txtReplacementText.Size = New System.Drawing.Size(470, 32)
Me.txtReplacementText.TabIndex = 8
Me.txtReplacementText.Text = ""
'
'Label3
'
Me.Label3.Location = New System.Drawing.Point(9, 280)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(111, 23)
Me.Label3.TabIndex = 7
Me.Label3.Text = "ReplacementText"
Me.Label3.TextAlign = System.Drawing.ContentAlignment.BottomLeft
'
'txtResults
'
Me.txtResults.Location = New System.Drawing.Point(10, 360)
Me.txtResults.Multiline = True
Me.txtResults.Name = "txtResults"
Me.txtResults.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
Me.txtResults.Size = New System.Drawing.Size(470, 64)
Me.txtResults.TabIndex = 10
Me.txtResults.Text = ""
'
'Label4
'
Me.Label4.Location = New System.Drawing.Point(9, 336)
Me.Label4.Name = "Label4"
Me.Label4.TabIndex = 9
Me.Label4.Text = "Results of Match"
Me.Label4.TextAlign = System.Drawing.ContentAlignment.BottomLeft
'
'cmdReplace
'
Me.cmdReplace.Location = New System.Drawing.Point(104, 432)
Me.cmdReplace.Name = "cmdReplace"
Me.cmdReplace.TabIndex = 12
Me.cmdReplace.Text = "Replace()"
'
'cmdTestRegex
'
Me.cmdTestRegex.Location = New System.Drawing.Point(16, 432)
Me.cmdTestRegex.Name = "cmdTestRegex"
Me.cmdTestRegex.TabIndex = 11
Me.cmdTestRegex.Text = "IsMatch()"
'
'cmdMatches
'
Me.cmdMatches.Location = New System.Drawing.Point(280, 432)
Me.cmdMatches.Name = "cmdMatches"
Me.cmdMatches.TabIndex = 14
Me.cmdMatches.Text = "Matches()"
'
'cmdSplit
'
Me.cmdSplit.Location = New System.Drawing.Point(192, 432)
Me.cmdSplit.Name = "cmdSplit"
Me.cmdSplit.TabIndex = 13
Me.cmdSplit.Text = "Split()"
'
'OpenFileDialog1
'
'
'SaveFileDialog1
'
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 15)
Me.ClientSize = New System.Drawing.Size(492, 466)
Me.Controls.Add(Me.cmdMatches)
Me.Controls.Add(Me.cmdSplit)
Me.Controls.Add(Me.cmdReplace)
Me.Controls.Add(Me.cmdTestRegex)
Me.Controls.Add(Me.txtResults)
Me.Controls.Add(Me.Label4)
Me.Controls.Add(Me.txtReplacementText)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.txtInputText)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.cmdSaveRegex)
Me.Controls.Add(Me.cmdOpenRegex)
Me.Controls.Add(Me.grpOptions)
Me.Controls.Add(Me.txtRegex)
Me.Controls.Add(Me.Label1)
Me.Font = New System.Drawing.Font("Tahoma", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.MaximizeBox = False
Me.MaximumSize = New System.Drawing.Size(500, 500)
Me.MinimizeBox = False
Me.MinimumSize = New System.Drawing.Size(500, 500)
Me.Name = "Form1"
Me.Text = "Regular Expression Tester"
Me.grpOptions.ResumeLayout(False)
Me.ResumeLayout(False)
End Sub
#End Region
'保存正则表达式
Private Sub cmdSaveRegex_Click()Sub cmdSaveRegex_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSaveRegex.Click
Me.SaveFileDialog1.ShowDialog()
End Sub
'保存文件
Private Sub SaveFileDialog1_FileOk()Sub SaveFileDialog1_FileOk(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk
Dim filePath As String = Me.SaveFileDialog1.FileName
Dim streamWriterRegex As StreamWriter = File.CreateText(filePath)
streamWriterRegex.Write(Me.txtRegex.Text)
streamWriterRegex.Close()
End Sub
'打开正则表达式
Private Sub cmdOpenRegex_Click()Sub cmdOpenRegex_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOpenRegex.Click
Me.OpenFileDialog1.ShowDialog()
End Sub
'打开文件
Private Sub OpenFileDialog1_FileOk()Sub OpenFileDialog1_FileOk(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
Dim filePath As String = Me.OpenFileDialog1.FileName
Dim streamReaderRegex As StreamReader = File.OpenText(filePath)
Me.txtRegex.Text = streamReaderRegex.ReadToEnd
streamReaderRegex.Close()
End Sub
'IsMatch 方法
Private Sub cmdTestRegex_Click()Sub cmdTestRegex_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTestRegex.Click
Try
'获得验证选项
Dim selectRegexOptions As RegexOptions = GetSelectedRegexOptions()
Dim testRegex As New Regex(Me.txtRegex.Text, selectRegexOptions)
If testRegex.IsMatch(Me.txtInputText.Text) Then
Me.txtResults.ForeColor = Drawing.Color.Red
Me.txtResults.Text = "Match Found"
Else
Me.txtResults.ForeColor = Drawing.Color.Black
Me.txtResults.Text = "No Match Found"
End If
Catch ex As ArgumentException '参数无效引发的异常
Me.txtResults.ForeColor = Drawing.Color.Red
Me.txtResults.Text = "There was an error in your regular expression when " _
& ControlChars.CrLf & ex.Message
End Try
End Sub
'Replace 方法
Private Sub cmdReplace_Click()Sub cmdReplace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdReplace.Click
Try
Dim selectRegexOptions As RegexOptions = GetSelectedRegexOptions()
Dim replaceRegex As New Regex(Me.txtRegex.Text, selectRegexOptions)
Me.txtResults.ForeColor = Drawing.Color.Black
Me.txtResults.Text = replaceRegex.Replace(Me.txtInputText.Text, Me.txtReplacementText.Text)
Catch ex As ArgumentException
Me.txtResults.ForeColor = Drawing.Color.Red
Me.txtResults.Text = "There was an error in your regular expression when " _
& ControlChars.CrLf & ex.Message
End Try
End Sub
'Split 方法
Private Sub cmdSplit_Click()Sub cmdSplit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSplit.Click
Try
Dim selectRegexOptions As RegexOptions = GetSelectedRegexOptions()
Dim splitRegex As New Regex(Me.txtRegex.Text, selectRegexOptions)
Dim splitResults() As String
Me.txtResults.ForeColor = Drawing.Color.Black
splitResults = splitRegex.Split(Me.txtInputText.Text)
Dim strElement As String
Dim strBuilder As New StringBuilder
For Each strElement In splitResults
strBuilder.Append(strElement & ControlChars.CrLf)
Next
Me.txtResults.Text = strBuilder.ToString()
Catch ex As ArgumentException
Me.txtResults.ForeColor = Drawing.Color.Red
Me.txtResults.Text = "There was an error in your regular expression when " _
& ControlChars.CrLf & ex.Message
End Try
End Sub
'Matches 方法
Private Sub cmdMatches_Click()Sub cmdMatches_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdMatches.Click
Try
Dim selectRegexOptions As RegexOptions = GetSelectedRegexOptions()
Dim matchesRegex As New Regex(Me.txtRegex.Text, selectRegexOptions)
Dim matchesFound As MatchCollection
Dim matchMade As Match
Dim strBuilder As New StringBuilder
matchesFound = matchesRegex.Matches(Me.txtInputText.Text)
Dim nextMatch As String = "----- Next Match -----" & ControlChars.CrLf
For Each matchMade In matchesFound
strBuilder.Append(matchMade.Value & ControlChars.CrLf)
strBuilder.Append(nextMatch)
Next
Me.txtResults.ForeColor = Drawing.Color.Black
Me.txtResults.Text = strBuilder.ToString()
Catch ex As Exception
Me.txtResults.ForeColor = Drawing.Color.Red
Me.txtResults.Text = "There was an error in your regular expression when " _
& ControlChars.CrLf & ex.Message
End Try
End Sub
'按选择项进行验证操作的方法
Private Function GetSelectedRegexOptions()Function GetSelectedRegexOptions() As RegexOptions
Dim selectRegexOptions As RegexOptions
If Me.chkECMAScript.Checked Then
selectRegexOptions = selectRegexOptions Or RegexOptions.ECMAScript
End If
If Me.chkExplicitCapture.Checked Then
selectRegexOptions = selectRegexOptions Or RegexOptions.ExplicitCapture
End If
If Me.chkIgnoreCase.Checked Then
selectRegexOptions = selectRegexOptions Or RegexOptions.IgnoreCase
End If
If Me.chkIgnorePatternWhiteSpace.Checked Then
selectRegexOptions = selectRegexOptions Or RegexOptions.IgnorePatternWhitespace
End If
If Me.chkMultiLine.Checked Then
selectRegexOptions = selectRegexOptions Or RegexOptions.Multiline
End If
If Me.chkRightToLeft.Checked Then
selectRegexOptions = selectRegexOptions Or RegexOptions.RightToLeft
End If
If Me.chkSingleLine.Checked Then
selectRegexOptions = selectRegexOptions Or RegexOptions.Singleline
End If
Return selectRegexOptions
End Function
End Class