vb.net 中的 Like 是否可以使用一系列负数和正数来进行模式匹配

Can a range of negative and positive numbers be used to pattern match using Like in vb.net

我有一个用户输入了字符串:

"{0.3064, 15.6497, 60.7668, 52.1362, 76.6645, 97, -15.8315, -6.8806, 5.547, -2.3381, -23.9905, 40.4569, 60.1592, 27.1418, 42.9375, -22.8297, -11.7423, -17.1576, -33.9918, 7.0585}" 

并且我希望能够根据开头有一个“{”,结尾有一个“}”以及逗号之间 -1000 和 1000 之间的二十个逗号分隔数字来验证它。

我试过使用下面的 like 运算符,但没有成功。 Everything returns False 不管它是否匹配模式。我如何根据此模式进行验证。

这是我试过的:

ArrayOk = ArrayValues1txt.Text Like "{[(1000)-1000],[(1000)-1000],[(1000)-1000],[(1000)-1000],[(1000)-1000],[(1000)-1000],[(1000)-1000],[(1000)-1000],[(1000)-1000],[(1000)-1000],[(1000)-1000],[(1000)-1000],[(1000)-1000],[(1000)-1000],[(1000)-1000],[(1000)-1000],[(1000)-1000],[(1000)-1000],[(1000)-1000],[(1000)-1000]}"
If ArrayOk = False Then
MsgBox("Wrong array pattern!  Pattern must contain 20 elements and be in the form {#,#}",
MessageBoxButtons.OK, "Bad Array Entered")
    GoTo Canceller
    Else
    End If

您可以使用 RegEx 验证以下一般模式:是否有 20 个十进制值,用逗号分隔并括在大括号中?

Dim pattern = "^{((-?\d+(\.\d+)?),\s?){19}(-?\d+(\.\d+)?)}$"
Dim input = "{0.3064, 15.6497, 60.7668, 52.1362, 76.6645, 97, -15.8315, -6.8806, 5.547, -2.3381, -23.9905, 40.4569, 60.1592, 27.1418, 42.9375, -22.8297, -11.7423, -17.1576, -33.9918, 7.0585}"

If (Regex.IsMatch(input, pattern)) Then

End If

验证它与特定模式匹配后,您可以通过执行以下操作来验证数字是否与数字范围验证匹配:

  1. 从字符串中删除括号
  2. 用逗号分隔字符串
  3. Trim 项目中的任何多余空格
  4. 将项目转换为分数值(双精度、十进制等)
  5. 检查物品是否在范围内

您可以使用 LINQ 执行许多这些步骤,这会使它更加简洁:

Dim pattern = "^{((-?\d+(\.\d+)?),\s?){19}(-?\d+(\.\d+)?)}$"
Dim input = "{0.3064, 15.6497, 60.7668, 52.1362, 76.6645, 97, -15.8315, -6.8806, 5.547, -2.3381, -23.9905, 40.4569, 60.1592, 27.1418, 42.9375, -22.8297, -11.7423, -17.1576, -33.9918, 7.0585}"

If (Regex.IsMatch(input, pattern)) Then
    input = input.Replace("{", String.Empty).Replace("}", String.Empty)
    Dim items = input.Split(",")
    Dim convertedItems = items.Select(Function(item) Convert.ToDouble(item.Trim())).ToArray()
    If (convertedItems.All(Function(item) item >= -1000 AndAlso item <= 1000)) Then
        Console.WriteLine("All of the items are within -1000 and 1000")
    End If
End If

这是一个现场演示:fiddle

我用 String.StartsWithString.EndsWith 来测试牙套。接下来我用 Substring 将它们切掉。然后我用逗号分割字符串。我检查了是否正好有20个元素

如果我们做到这一点,我将遍历每个元素。 Trim 关闭任何前导或尾随空格并将其更改为双精度。然后我可以做数字比较。

Private Function ValidateArray(input As String) As Boolean
    If Not input.StartsWith("{") OrElse Not input.EndsWith("}") Then
        Return False
    End If
    input = input.Substring(1, input.Length - 2)
    Dim splits = input.Split(","c)
    If splits.Length <> 20 Then
        Return False
    End If
    For Each n In splits
        Dim dbl = CDbl(n.Trim)
        If dbl > 1000 OrElse dbl < -1000 Then
            Return False
        End If
    Next
    Return True
End Function

用法:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim IsValid = ValidateArray("{0.3064, 15.6497, 60.7668, 52.1362, 76.6645, 97, -15.8315, -6.8806, 5.547, -2.3381, -23.9905, 40.4569, 60.1592, 27.1418, 42.9375, -22.8297, -11.7423, -17.1576, -33.9918, 7.0585}")
    MessageBox.Show(IsValid.ToString)
End Sub