2016-12-06 76 views
0

此方法返回true时,它不应该如何匹配模式和“喜欢”在.NET运营商逃避起誓括号([])

pattern = "*[[email protected]]*" 
file = "test.pdf" 
    If file Like pattern Then 
     MsgBox("like method, file: " & file) 
    End If 

每个文件我放到var文件,我得到在这一行真:

If file Like pattern Then 

其真应该出现在

file = "P0LRBE2RUK[[email protected]].awes0me" 

所以,我怎么能逃脱方括号([])的海RCH模式(不是正则表达式)

BTW我有图案的名单从API(https://fsrm.experiant.ca/),我不能使用:

For Each file In IO.Directory.GetFiles(path, pattern,IO.SearchOption.AllDirectories) 

因为IO.Directory.GetFiles抛出一个异常否认文件夹

回答

0

不是最好的方法,但工程

Function WildcardToRegex(pattern As String) As String 
    Return "^" + Regex.Escape(pattern).Replace("\*", ".*").Replace("\?", ".") + "$" 
End Function 

Function wildcardRegexMatch(wildcard As String, s As String) As Boolean 
    Dim regexPattern As Regex = New Regex(WildcardToRegex(wilcard)) 
    Return regexPattern.IsMatch(s) 
End Function 

:d