Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looking for simple yet powerful windows wildcards (`*, ?`) matching implementation

I'm looking for simple and powerful way to implement Windows flavoured * and ? wildcards matching in strings.

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

A happy medium wanted.

EDIT: I'm trying to parse .gitignore file and match the same files, as Git does. This means:

  • File should be out of repository's index (so I'm checking file's path against one stored in index)
  • Number of patterns in .gitignore can be large;
  • Number of files to check might also be large.
like image 461
shytikov Avatar asked Feb 22 '23 20:02

shytikov


1 Answers

The equivalents of the Windows wildcards ? and * in regex are just . and .*.


[Edit] Given your new edit (stating that you're looking for actual files), I would skip the translation altogether and let .Net do the searching using Directory.GetFiles().

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

like image 117
BlueRaja - Danny Pflughoeft Avatar answered Apr 06 '23 01:04

BlueRaja - Danny Pflughoeft