views:

1535

answers:

3

I've got a wildcard pattern, perhaps "*.txt" or "POS??.dat".

I also have list of filenames in memory that I need to compare to that pattern.

How would I do that, keeping in mind I need exactly the same semantics that IO.DirectoryInfo.GetFiles(pattern) uses.

EDIT: Blindly translating this into a regex will NOT work.

A: 

Just use a Regex class. Initialize it with the wildcard pattern that you are thinking of and then use the .IsMatch(filename) method to check each filename to see if it matches.

Sanjay Sheth
most regexes use different syntax than the typical shell globbing the OP is talking about. I don't know if that's the case in .net or not.
rmeador
You can't just blindly translate wildcard patterns into regular expressions.
Jonathan Allen
Actually, I was aware that you can't translate wildcard patterns into regex .. I just assumed the OP would understand he needs to translate *.txt to the equivalent regex when he would use the regex class.
Sanjay Sheth
+3  A: 

You could translate the wildcards into a regular expression:

*.txt -> ^.+\.txt$

POS??.dat _> ^POS..\.dat$

Use the Regex.Escape method to escape the characters that are not wildcars into literal strings for the pattern (e.g. converting ".txt" to "\.txt").

The wildcard * translates into .+, and ? translates into .

Put ^ at the beginning of the pattern to match the beginning of the string, and $ at the end to match the end of the string.

Now you can use the Regex.IsMatch method to check if a file name matches the pattern.

Guffa
-1 because this answer is just flat wrong. It almost works for the two examples posted, except you need to make sure that the regex is made case-insensitive. But the behavior of GetFiles is rather complex. See http://msdn.microsoft.com/en-us/library/8he88b63.aspx for details.
Jim Mischel
Thanks for the attempt, but like I said it needs to match GetFiles exactly and this won't.
Jonathan Allen
Well, it's not possible to get the same behaviour as GetFiles with a list of file names, as it's impossible to know what the short file names were.
Guffa
+2  A: 

Some kind of regex/glob is the way to go, but there are some subtleties; your question indicates you want identical semantics to IO.DirectoryInfo.GetFiles. That could be a challenge, because of the special cases involving 8.3 vs. long file names and the like. The whole story is on MSDN.

If you don't need an exact behavioral match, there are a couple of good SO questions:

http://stackoverflow.com/questions/188892/glob-pattern-matching-in-net
http://stackoverflow.com/questions/398518/how-to-implement-glob-in-c

zweiterlinde