Re: I need a test to see that a string is a valid path to a file
- From: "AAaron123" <aaaron123@xxxxxxxxxxxxxx>
- Date: Wed, 25 Feb 2009 12:43:52 -0500
"Hans Kesting" <news.hansdk@xxxxxxxxxxxxxxx> wrote in message
news:eXSC9q0lJHA.4028@xxxxxxxxxxxxxxxxxxxxxxx
AAaron123 explained :That's obvious immediately after looking at the expression above. Don't know
I know this isn't a regular expression NG but there does not appear to be
one in the microsoft series which is all my news reader shows.
I need a test to see that a string is a valid path to a file.
What do you mean?
"does point to an existing file": you can't check that with a regex.
"seems like it could be a valid file": that is possible
The string does not have a drivename nor a filename, simply a collection
of folder names, but I could add that for the test.
I don't know anything about regular expressions so I looked on the
Internet and found some.
The following two are quite different in length.
I'm hoping someone with a little experience can help me select one or
maybe share the one they use.
One other question: Can I use any regular expression or must it be one
directed to visual studio languages. That is, is there more than one
specification for writing regular expressions?
There are various flavours or dialects.
For instance javascript (in a browser) understands just a subset of what
is available to .Net languages (javascript would not understand your
examples). But I think most of the features are pretty standardized.
Thanks in advance
//Matches filenames
//^([a-zA-Z]\:)(\\[^\\/:*?<>"|]*(?<![ ]))*(\.[a-zA-Z]{2,6})$
Accepts just a 2 to 6 letter extension ("file.c" would fail, as would
"something.a1").
Also it needs to start with a drive letter, which you didn't want/need.
//Matches drives, folders and file on a Windows OS. Folder matches must
end with \ Folder and file names can not end with a space. I limited the
file extension length to 15 though that not a restriction of a file's
extension This is a mod of Darren's regex
http://www.regexlib.com/REDetails.aspx?regexp_id=357 and my own
http://www.regexlib.com/REDetails.aspx?regexp_id=137 Updated Feb 2005
//^((?:[a-zA-Z]:)|(?:\\{2}\w[-\w]*)\$?)\\(?!\.)((?:(?![\\/:*?<>"|])(?)[\x20-\x7E])+\\(?!\.))*((?:(?:(?![\\/:*?<>"|])(?![
.]$)[\x20-\x7E])+)\.((?:(?![\\/:*?<>"|])(?![ .]$)[\x20-\x7E]){2,15}))?$
This required a 2-15 character extension and maybe supports too many
characters for directory- and filenames (everything from hexcode 20 up to
hexcode 7E, which does include illegal characters).
how I missed that.
Just kidding!
No, I'm not fixed to regex simply thought of that as an approach. But Mike's
appears better. My reply to Mike shows I still have issues; maybe you can
help again by commenting on one of them. I'd sure apppreciate it.
Thanks a lot
The code that Mike gave looks OK. Or do you really need a regex?
Hans Kesting
.
- References:
- I need a test to see that a string is a valid path to a file
- From: AAaron123
- Re: I need a test to see that a string is a valid path to a file
- From: Hans Kesting
- I need a test to see that a string is a valid path to a file
- Prev by Date: Re: I need a test to see that a string is a valid path to a file
- Next by Date: Re: Detecting if a file is already open
- Previous by thread: Re: I need a test to see that a string is a valid path to a file
- Next by thread: autoexec.nt running C# console application on startup of computer
- Index(es):
Relevant Pages
|