regular expression help cc

G

Guest

hey all,

what would my expression look like if i wanted to make sure that the input
matched the following pattern.

c:\filename.ext

it doesn't have to be the c drive just a letter, colon, filename, and
extension.

thanks,
rodchar
 
J

Jay B. Harlow [MVP - Outlook]

rodchar,
Are you wanting a Regular expression that matches any file name in any
folder?

Or one that specifically matches a file name in a specified root folder?

Do you want to support optional extensions?

Do you want to support only DOS file names (8.3) or long file names?

Do you want to support optional path separators? (both forward & backward
slashes)

Do you want to support UNC names?


The following matches 8.3 names in the root folder:

Const pattern As String = "^[a-zA-z]:\\(\w+)\.(\w+)$"
Static fileNameRegex As New Regex(pattern, RegexOptions.Compiled)
Debug.WriteLine(fileNameRegex.IsMatch("c:\filename.txt"))
Debug.WriteLine(fileNameRegex.IsMatch("c:\filename"))
Debug.WriteLine(fileNameRegex.IsMatch("c:\.txt"))
Debug.WriteLine(fileNameRegex.IsMatch("filename.txt"))
Debug.WriteLine(fileNameRegex.IsMatch("filename"))

The pattern "^[a-zA-z]:\\(\w+)\.(\w+)$" will match an upper or lower case
ASCII letter, followed by a colon, followed by a backward slash, followed by
one or more letters, followed by a period, followed by one or more letters.
The entire pattern needs to be the only thing in the string.


Unfortunately there is no clear RegEx newsgroup. In addition to the
microsoft.public.dotnet.general newsgroup, I would recommend
microsoft.public.dotnet.framework. However! Asking here is probably just as
good in most cases!

A tutorial & reference on using regular expressions:
http://www.regular-expressions.info/

The MSDN's documentation on regular expressions:
http://msdn.microsoft.com/library/d...l/cpconRegularExpressionsLanguageElements.asp

Hope this helps
Jay


Hope this helps
Jay
 

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