473,385 Members | 1,782 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Pattern matching filenames

Hi,

C#, from a FileSystemWatcher I would like to catch all files with a *.*
filter but then inside the event handler compare against a list of wildcards
(eg '*.abc;*.def')

Is there anywhere inside the Framework where I can do something like

Pattern x = new Pattern("*.abc");
if ( x.Matches(strFilename) )
{
//do something
}

Obviously I could just assume the file extensions were being used (and use
Path.GetExtension accordingly), but if a wildcard such as 'xyz??.*' is used
then I havent a hope.

Hope you can help, thanks

BTW:
The Regex syntax is different from filename
wildcards.

I could write a conversion from filename wildcard to regex syntax (eg. ? to
..?) but it would be fragile to change and possibly buggy.

Surely there must a simple way of doing this?

Thanks for the help

p.s. If you cannot help me then you force me to write new code of my own!!

Sea
Nov 15 '05 #1
1 6990
"Sea Sharper" <xx@xx.co.uk> wrote in
news:bq*******************@news.demon.co.uk:
Hi,

C#, from a FileSystemWatcher I would like to catch all files
with a *.* filter but then inside the event handler compare
against a list of wildcards (eg '*.abc;*.def')

Is there anywhere inside the Framework where I can do something
like

Pattern x = new Pattern("*.abc");
if ( x.Matches(strFilename) )
{
//do something
}

Obviously I could just assume the file extensions were being
used (and use Path.GetExtension accordingly), but if a wildcard
such as 'xyz??.*' is used then I havent a hope.

Hope you can help, thanks

BTW:
The Regex syntax is different from filename
wildcards.

I could write a conversion from filename wildcard to regex
syntax (eg. ? to .?) but it would be fragile to change and
possibly buggy.


Sea,

Why would converting to the regex syntax be buggy or fragile?

The DOS wildcard spec only has two metacharacters - * and ?. Those
are easy to replace. Here's how I would approach the problem:
1. Escape the input string.

string pattern = Regex.Escape("xyz??.*");

// pattern will now look like "xyz\?\?\.\*"
2. Replace all "\?" with .

pattern = pattern.Replace(@"\?", ".");

// pattern now looks like this: "xyz..\.\*"
3. Replace all "\*" with .*

pattern = pattern.Replace(@"\*", ".*");

// pattern now looks like this: "xyz..\..*"
If you want to completely violate the Law of Demeter, you could
combine all three steps like this:

string pattern = Regex.Escape("xyz??.*").Replace(@"\?",
".").Replace(@"\*", ".*");

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: gsv2com | last post by:
One of my weaknesses has always been pattern matching. Something I definitely need to study up on and maybe you guys can give me a pointer here. I'm looking to remove all of this code and just...
176
by: Thomas Reichelt | last post by:
Moin, short question: is there any language combining the syntax, flexibility and great programming experience of Python with static typing? Is there a project to add static typing to Python? ...
9
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # Matching string patterns # # Sometimes you want to know if a string is of # particular pattern. Let's say in your website # you have converted all images...
1
by: Henry | last post by:
I have a table that stores a list of zip codes using a varchar column type, and I need to perform some string prefix pattern matching search. Let's say that I have the columns: 94000-1235 94001...
2
by: Joecx | last post by:
Hi If I want to copy files using a pattern like: I want all files on a directory that start with 20050822 to be copied to a different directory. I can't get file.copy or copyfile to accept *.*...
10
by: bpontius | last post by:
The GES Algorithm A Surprisingly Simple Algorithm for Parallel Pattern Matching "Partially because the best algorithms presented in the literature are difficult to understand and to implement,...
5
by: olaufr | last post by:
Hi, I'd need to perform simple pattern matching within a string using a list of possible patterns. For example, I want to know if the substring starting at position n matches any of the string I...
2
by: Ole Nielsby | last post by:
First, bear with my xpost. This goes to comp.lang.c++ comp.lang.functional with follow-up to comp.lang.c++ - I want to discuss an aspect of using C++ to implement a functional language, and...
1
by: VanKha | last post by:
I write this program for pattern-matching,but it gives wrong result: #include<iostream> #include<conio.h> #include<string.h> using namespace std; main() { char text,pat;...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.