2016-12-06 26 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