Regex 检验器

来源:互联网 发布:双飞姐妹俩体验知乎 编辑:程序博客网 时间:2024/04/30 10:33

最近学习一下正则,顺便把书里的例子抄袭一下。^ ^ ...

 

Imports System.IO
Imports System.Text
Imports System.Text.RegularExpressions

Public Class Form1
    
Inherits System.Windows.Forms.Form

Windows 窗体设计器生成的代码

    
'保存正则表达式
    Private 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(ByVal sender As ObjectByVal 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(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOpenRegex.Click
        
Me.OpenFileDialog1.ShowDialog()
    
End Sub


    
'打开文件
    Private Sub OpenFileDialog1_FileOk(ByVal sender As ObjectByVal 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(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(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(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(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() 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

 

 

 

原创粉丝点击