Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net Net正则表达式帮助_Vb.net_Regex - Fatal编程技术网

Vb.net Net正则表达式帮助

Vb.net Net正则表达式帮助,vb.net,regex,Vb.net,Regex,我有3或4个模式,我正在比较用户输入,我需要找出用户输入是否匹配其中一个模式,如果匹配,则返回匹配 由于输入是多行的,因此我将按如下方式传递每一行: Dim strRawInput() As String = Split(txtInput.Text, vbCrLf) Dim strInput As String txtOutput.Text = "" For Each strInput In strRawInput strInput.Trim(

我有3或4个模式,我正在比较用户输入,我需要找出用户输入是否匹配其中一个模式,如果匹配,则返回匹配

由于输入是多行的,因此我将按如下方式传递每一行:

    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
然后我要找到匹配项:

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
        txtOutput.Text = txtOutput.Text & "Success: " & m.ToString & vbCrLf
        Exit Sub 
    Else

    End If
Next
txtOutput.Text = txtOutput.Text & "Not this time" & vbCrLf
我怎样才能更有效地做到这一点?此外,我在那里添加了Exit子元素,以避免在找到匹配项后显示“notthist time”(不是这次)消息(如果匹配项与数组中的一个更晚的模式匹配),但我也希望找到更好的方法


提前感谢。

不要在循环中生成正则表达式,而是在应用程序启动时一次性生成它们。所以可能是这样的:

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
然后您可以将其他代码更改为:

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
关于
Exit Sub
,我认为这很好,您已经发现它至少匹配一个模式,那么为什么还要继续评估其余的模式呢。但是,如果您不喜欢可以在多个位置“返回”的方法,您可以将其替换为
布尔值和
退出,如下所示:

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

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

嗯,如果是这样的话,这不是更好吗

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

我可以将m as匹配声明也保留在循环之外吗?@Radu:你是指实际的
Dim
ing吗?如果是这样的话,你可以把它放在循环外,但是因为你在循环内初始化它,我不确定它会更快。我只是把它放在循环中,因为我喜欢让任何变量声明尽可能接近它们的实际用途。是的,我只是认为声明一次,并在每次使用中赋值会比在每个循环中声明和赋值更快。@Radu:我相当确定这不会有太大的区别(至少在大多数情况下),但是,如果你真的需要从中挤出所有的表现,你可能必须根据你的情况来衡量,以确保。这个问题似乎表明,如果在循环中声明,它有时甚至可以更快,但它取决于许多因素。