0

我需要另一双眼睛。我一直在使用这种 LINQ 语法来使用正则表达式扫描哈希表。似乎无法完全正确。目标是将所有键与正则表达式匹配,然后使用这些结果将剩余值与单独的正则表达式匹配。在下面的测试用例中,我应该以前三个条目结束。

Private ReadOnly Property Testhash As Hashtable
    Get
        Testhash = New Hashtable
        Testhash.Add("a1a", "abc")
        Testhash.Add("a2a", "aac")
        Testhash.Add("a3a", "acc")
        Testhash.Add("a4a", "ade")
        Testhash.Add("a1b", "abc")
        Testhash.Add("a2b", "aac")
        Testhash.Add("a3b", "acc")
        Testhash.Add("a4b", "ade")
    End Get
End Property

Public Sub TestHashSearch()

    Dim KeyPattern As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex("a.a")
    Dim ValuePattern As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex("a.c")

    Try
        Dim queryMatchingPairs = (From item In Testhash
                                  Let MatchedKeys = KeyPattern.Matches(item.key)
                                  From key In MatchedKeys
                                  Let MatchedValues = ValuePattern.Matches(key.value)
                                  From val In MatchedValues
                                  Select item).ToList.Distinct

        Dim info = queryMatchingPairs

    Catch ex As Exception

    End Try

End Sub
4

2 回答 2

1

你不能同时匹配键和值吗?

Dim queryMatchingPairs = (From item In Testhash
                          Where KeyPattern.IsMatch(item.Key) And ValuePattern.IsMatch(item.Value) 
                          Select item).ToList
于 2013-08-06T13:20:31.963 回答
0

我应该早点休息一下,然后多工作一点。正确的解决方案在第二个正则表达式中使用原始的“from item”而不是较低的“from key”。此外,哈希表不需要“distinct”。

            Dim queryMatchingPairs = (From item In Testhash
                                      Let MatchedKeys = KeyPattern.Matches(item.key)
                                      From key In MatchedKeys
                                      Let MatchedValues = ValuePattern.Matches(item.value)
                                      From val In MatchedValues
                                      Select item).ToList
于 2013-08-06T13:15:47.270 回答