通配符搜索字典 [英] Wildcard search of dictionary

查看:99
本文介绍了通配符搜索字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

搜索google和SO后,我看到有一种方法可以搜索现有密钥的字典:

After searching google and SO, I see that there is a way for me to search a dictionary for an existing key:

dict.exists("search string")

我的问题是如何使用通配符:

My question is how can I search a dictionary using a wildcard:

dict.exists("search*")

我想首先搜索词典,因为我的宏有用户选择一组文件(文件名作为字典键,完整路径作为值)并且我想确定在组中是否存在某个命名约定的文件,然后我迭代字典元素以应用文件处理。

I want to search the dictionary for a term first because my macro has the user select a group of files (the file name as dictionary key and the full path as the value) and I want to determine if files of a certain naming convention are present in the group BEFORE I iterate the dictionary elements to apply the file processing.

如果找到某个命名约定,则在字典中的每个文件上执行X处理,而不是Y处理。诀窍在于,如果任何元素遵循某些命名约定,那么它们都需要相应的处理。也就是说,如果元素1-19不符合约定但是20遍,则所有元素1-20需要特定的处理。这是我不能一次检查每个名称的原因,并且一次选择性地处理一个文件。

If a certain naming convention is found, X processing is run on each file in the dictionary instead of Y processing. The trick is that if ANY of the elements follow the certain naming convention, then they all need to be processed accordingly. That is to say, if elements 1-19 fail to meet the convention but 20 passes, then all elements 1-20 need specific processing. This is the reason I cant just check each name as I go and process selectively one file at a time.

我目前的解决方案是在搜索整个字典后重复命名约定,然后在知道在处理文件中使用哪种方法之后重申字典。我正在循环所有的元素两次,看起来不高效...

My current solution is to iterate the entire dictionary once searching for the naming convention, then reiterating the dictionary after I know which method to use in processing the files. I am looping through all the elements twice and that doesn't seem efficient...

你们有合理的解决方案来通配符搜索字典键吗?

Do you guys have a reasonable solution for wildcard searching the dictionary keys?

推荐答案

Dictionary Items方法返回所有项目的数组。您可以将它们加入一个大字符串,然后使用 Instr()来确定您的搜索字符串是否在大字符串中。

The Dictionary Items method returns an array of all the items. You can Join those into a big string then use Instr() to determine if your search string is in the big string.

从你的例子中你最后有星号,所以我假设你关心一个项目的启动,而不是一个子字符串存在于任何地方。所以我寻找分隔符+子字符串,并将分隔符添加到 Join 的前面(为了第一个项目的目的)。如果您有不同的要求,则必须进行调整,但理论是一样的。

From your example, you have the asterisk at the end, so I'm assuming you care how an item starts, not that a sub-string exists anywhere. So I look for delimiter+substring and add the delimiter to the front of the Join (for the sake of the first item). If you have different requirements, you'll have to adjust, but the theory is the same.

我使用了两个管道作为分隔符,因为它不太可能在数据中并返回假阳性。这可能不适合您的数据。

I used two pipes as a delimiter because it's unlikely to be in the data and return a false positive. That may not be appropriate for your data.

Public Function WildExists(ByRef dc As Scripting.Dictionary, ByVal sSearch As String) As Boolean        
    Const sDELIM As String = "||"        
    WildExists = InStr(1, sDELIM & Join(dc.Keys, sDELIM), sDELIM & sSearch) > 0        
End Function

测试代码

Sub Test()

    Dim dc As Scripting.Dictionary            
    Set dc = New Scripting.Dictionary

    dc.Add "Apple", "Apple"
    dc.Add "Banana", "Banana"
    dc.Add "Pear", "Pear"

    Debug.Print WildExists(dc, "App") 'true
    Debug.Print WildExists(dc, "Ora") 'false

End Sub

这篇关于通配符搜索字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆