开发者

VB.Net Regex Help

I've got 3 or 4 patterns that I'm comparing user input against and I need to figure out if the user input matches one of the patters and to return the match if it does.

Since the input is multiline, I'm passing each individual line like so:

    Dim strRawInput() As String = Split(txtInput.Text, vbCrLf)
    Dim strInput As String

    txtOutput.Text = ""

    For Each strInput In strRawInput
        strInput.Trim(vbCr, vbLf, Chr(32))
        Validation(strInput)
    Next

Then I have this to find matches:

Dim m As Match

For i = 0 To strValidator.Length - 1
    Dim r As New Regex(strValidator(i))
    m = r.Match(strInput)

    If m.Success Then
        txtOutp开发者_如何转开发ut.Text = txtOutput.Text & "Success: " & m.ToString & vbCrLf
        Exit Sub 
    Else

    End If
Next
txtOutput.Text = txtOutput.Text & "Not this time" & vbCrLf

How can I do this more efficiently? Also, I added the Exit Sub there to avoid showing the "Not this time" message even after a match is found (if the match is with one of the later patterns in the array), but I'd like to find a better way of doing that too.

Thanks in advance.


Rather than generating your Regexs in the loop, generate them one time at the startup of the application. So maybe something like:

Private Shared m_regexes As New List(Of Regex)
Shared Sub New()
    For Each v As String In strValidator
        m_regexes.Add(New Regex(v))
    Next
End Sub

And then you can change your other code to:

For Each r As Regex In m_regexes
    Dim m As Match = r.Match(strInput)
    If m.Success Then
        txtOutput.Text = txtOutput.Text & "Success: " & m.ToString & vbCrLf
        Exit Sub
    Else

    End If
Next

Regarding the Exit Sub, I think it's fine, You've discovered that it matches at least one pattern, so why continue to evaluate the rest. But if you don't like methods that can "return" in multiple places, you could just replace it with a Boolean and a Exit For as:

Dim found as Boolean = false 
For Each ...
     If IsMatch Then
          found = True
          Exit For
     End If
 Next

 If Found Then
       ....
 Else
       .....
 End If


Hmm if that's the case then wouldn't this be even better?

For i = 0 To strValidator.Length - 1
    Dim r As New Regex(strValidator(i))
    Dim m As Match = r.Match(strInput)

    If m.Success Then
        txtOutput.Text = txtOutput.Text & "Success: " & m.ToString & vbCrLf
        Exit Sub 
    End If
Next
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜