Matching a string to a filename mask?

B

Brian Parker

I have an ArrayList ( strings ) of filenames like such:

fred.out
inctructions.txt
luncheon01.jpg
luncheon02.jpg
luncheon05.jpg
luncheon11.jpg
HeebieJeebie.cs
...etc..

The user will provide a filename mask such as:
*.*
*.jpg
luncheon0?.*
...etc..

Is there a simple way to do a comparison of the user-defined filename
masks -vs- a string? I need to return a list of the strings that
matched their filename mask.

The users will not know how to create RegEx expressions, only typical
filename masks.

At worst, I think I can convert their mask to a RegEx and match/test on
that, but was hoping there was an easier way.

-BEP
 
Y

Yury

1) If you create your list from real files. You can use following
static method to get filtered list next time.
Directory.GetFiles(string Path, string pattern)

2) You can easily convert ? and * mask to regex:
Regex.Escape(pattern).Replace(@"\*", ".*").Replace(@"\?",".")
 
B

Brian Parker

Yury said:
1) If you create your list from real files. You can use following
static method to get filtered list next time.
Directory.GetFiles(string Path, string pattern)

The filenames are cached already so I won't be able to do this. I use
GetFiles() to get the original list, though.
2) You can easily convert ? and * mask to regex:
Regex.Escape(pattern).Replace(@"\*", ".*").Replace(@"\?",".")

I tried this and it's -really- close to what I need. I can probably get
it the rest of the way.

Thanks for the reply.

-BEP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top