473,397 Members | 2,099 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,397 software developers and data experts.

RegEx and filenames

I have a list of file names I need to parse and check if they match a
valid expression.

I want this to work like you were listing a directory.

ie

*.doc dhows all doc
*.* shows all documents
*doc shows all those ending with doc
?abc.doc would show documents aabc.doc, babc.doc etc

etc
Mar 31 '07 #1
2 2076
Jeff Williams wrote:
I have a list of file names I need to parse and check if they match a
valid expression.

I want this to work like you were listing a directory.

ie

*.doc dhows all doc
*.* shows all documents
*doc shows all those ending with doc
?abc.doc would show documents aabc.doc, babc.doc etc
This might be of some interest to you:
http://www.codeproject.com/cs/files/...Enumerator.asp

If you're just interested in the regex stuff, this is roughly what you need:

// trim whitespace off file spec and convert Win32 wildcards to
regular expressions
string pattern = spec
.Trim()
.Replace(".", @"\.")
.Replace("*", @".*")
.Replace("?", @".?")
;

Regex re = new Regex("^" + pattern + "$", RegexOptions.IgnoreCase);

-cd
Mar 31 '07 #2
Use the System.IO.Directory.GetFiles(string, string) method. The first
parameter is the path, and the second is the search pattern, which works
just like the file system.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Printing Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

"Jeff Williams" <je***********@hardsoft.com.auwrote in message
news:13*************@corp.supernews.com...
>I have a list of file names I need to parse and check if they match a valid
expression.

I want this to work like you were listing a directory.

ie

*.doc dhows all doc
*.* shows all documents
*doc shows all those ending with doc
?abc.doc would show documents aabc.doc, babc.doc etc

etc

Apr 2 '07 #3

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

Similar topics

1
by: Alasdair | last post by:
Hi, I have a problem using regular expresions and the glob operator. I want to get files with a specific name format from a directory using glob. The regular expresion I use to match the name...
16
by: Andrew Baker | last post by:
I am trying to write a function which provides my users with a file filter. The filter used to work just using the VB "Like" comparision, but I can't find the equivilant in C#. I looked at...
2
by: rbutch | last post by:
guys, i need a little help with this. this is working (well sort of) i get the info, but it's not moving to a new line as it iterates thru the array and all of the fields are like ONE HUGE LONG...
0
by: n33470 | last post by:
We have a web site that is being converted from the 1.1 format into 2.0. I've noticed that after the web project has been converted, the first time that the solution is opened in VS, all of the...
5
by: skavan | last post by:
Hi, I'm just wrapping my head around regex and am pretty sure it can do the task at hand - but it's too complex for my brain to process -- so am throwing it out there for you experts to comment...
4
by: skavan | last post by:
Use Case: We have music files that describe, in their filename, attributes of the music. We do not know a general pattern that applies to all filenames -- but we do know that filenames that are...
0
by: chongming | last post by:
Hi, i want to display all the filenames on browser. However i found that if there are many filenames in that folder, result will be it will display a long list of filenames on that browser. My...
4
by: pedrito | last post by:
I have a regex question and it never occurred to me to ask here, until I saw Jesse Houwing's quick response to Phil for his Regex question. I have some filenames that I'm trying to parse out of...
1
by: =?Utf-8?B?QnJpYW4=?= | last post by:
i have the following regex: Regex r = new System.Text.RegularExpressions.Regex(@"LES_GAS{8}\-{6,7}\.DEL"); I expected it to match filenames like the following: LES_GAS20080718-0301826.DEL ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.