473,791 Members | 3,193 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pattern matching filenames

Hi,

C#, from a FileSystemWatch er 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(strFi lename) )
{
//do something
}

Obviously I could just assume the file extensions were being used (and use
Path.GetExtensi on 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 7028
"Sea Sharper" <xx@xx.co.uk> wrote in
news:bq******** ***********@new s.demon.co.uk:
Hi,

C#, from a FileSystemWatch er 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(strFi lename) )
{
//do something
}

Obviously I could just assume the file extensions were being
used (and use Path.GetExtensi on 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("x yz??.*");

// 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("x yz??.*").Replac e(@"\?",
".").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
6982
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 use pattern matching to determine if the proper amount of numeric characters has been met. Here is the function I've already done. Any help you can give in a pattern matching solution would be much appreciated and very educational.
176
8191
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? Thank you, -- greetz tom
9
3218
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 files from gif # format to png format. Now you need to change the # html code to use the .png files. So, essentially
1
2739
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 94100 If I run a pattern matching search for the string "940", then I should get the top two rows of data back. If I run a pattern matching search for the string "94", then I should get all the three rows of data back.
2
342
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 *.* pattern matching. Does anyone have the code to make this work? Thanx if you can help!
10
4985
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, knowledge of fast and practical algorithms is not commonplace." Hume and Sunday, "Fast String Searching", Software - Practice and Experience, Vol. 21 # 11, pp 1221-48
5
5760
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 have a list, as below: sentence = "the color is $red" patterns = pos = sentence.find($)
2
3398
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 I'd like the attention of fp as well as C++ gurus if available. The language I'm implementing - PILS - is dynamically
1
2771
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; cout<<"text:";cin>>text;
0
9669
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9515
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10427
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10155
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9995
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.