Re: I need a test to see that a string is a valid path to a file

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




"Hans Kesting" <news.hansdk@xxxxxxxxxxxxxxx> wrote in message
news:eXSC9q0lJHA.4028@xxxxxxxxxxxxxxxxxxxxxxx
AAaron123 explained :
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](?:\\|$))[\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).

That's obvious immediately after looking at the expression above. Don't know
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








.



Relevant Pages

  • Re: I need a test to see that a string is a valid path to a file
    ... I need a test to see that a string is a valid path to a file. ... "does point to an existing file": you can't check that with a regex. ... I don't know anything about regular expressions so I looked on the Internet and found some. ... Folder matches must end with \ Folder and file names can not end with a space. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: I need a test to see that a string is a valid path to a file
    ... 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. ... I can't help with regex, but this seems easier to me: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Search for multiple things in a string
    ... >> I also feel that Regular Expressions, being an object in asp.net (not ... So using Regex is not really like using another language (as C# is different ... I agree with you that readability is important. ... And I was not saying experiment with it. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: for a laught (???)
    ... Regex doesn't work too well with a null byte delimiter :-) ... the API for a particular form of regular expressions ... Regex doesn't work with null terminated strings. ... qualifier or the qualifier "commonly" might have suggested. ...
    (comp.lang.cobol)
  • Re: [OT] Re: Chris Sonnack on VB.Nets putative Set statement
    ... > regex is in a C-ish language string. ... the ease of making errors in regular expressions is a concern ... You do need to replicate the pattern on either side of the comma ...
    (comp.programming)