Search for simple but powerful Windows wildcard windows (`* ,?`) matching implementation

I'm looking for a simple and effective way to implement wildcard combinations * and ? with windows support in rows.

BeginsWith() , EndsWith() too simple to cover all cases, while translating wildcards into regex'es will look complicated, and I'm not sure about the performance.

Desired happy person.

EDIT: I am trying to parse a .gitignore file and map the same files as Git. It means:

  • The file must be outside the storage index (so I check the path to the file stored in the index)
  • The number of templates in .gitignore can be large;
  • The number of files to check can also be large.
+4
source share
4 answers

Windows wildcard equivalents ? and * in regular expression is easy . and .* .


[Edit] Given your new editing (stating that you are looking for the actual files), I would generally skip the translation and allow .Net to search using Directory.GetFiles() .

(note that for some reason, passing ? in Directory.GetFiles() matches "zero or one character", whereas on Windows it always matches exactly one character)

+4
source

To get an exact match, including all angle scales, use

 System.IO.Directory.GetFiles(myPath, myPattern) 

You may need to create some temporary files that form your target lines.

In other words, I think you should keep your templates dry until you come across a file.

+2
source

Convert * and? for regular expression is simple enough.
For? replace "?" with ". {1}" and for * replace "*" with ". +?"

This should lead to the same behavior as wildcards in windows.

EDIT: boolean PathMatchSpec (input, template) will do the job.

 Private Declare Auto Function PathMatchSpec Lib "shlwapi" (ByVal pszFileParam As String, ByVal pszSpec As String) As Boolean 
+1
source

You should use a regex approach if your data volume is not humungous or you have data points to say that regex will greatly affect performance.

If so, any other solution is also likely to affect performance, and you probably have to do something.

+1
source

Source: https://habr.com/ru/post/1390725/


All Articles