Validating file names using regex

Hi there, I am sure what I am looking for exist somewhere but I haven't been able to find anything that works in boost::regex. What I am looking for is a regular expression that will validate a filename according to the specification of the OS (UNIX systems and Windows). I would be very grateful if someone could provide me with these two expressions (one for Linux and one for Windows). Later,

Homework? What have you tried .... with what results? Jean-Sebastien Vachon wrote:
Hi there,
I am sure what I am looking for exist somewhere but I haven't been able to find anything that works in boost::regex.
What I am looking for is a regular expression that will validate a filename according to the specification of the OS (UNIX systems and Windows).
I would be very grateful if someone could provide me with these two expressions (one for Linux and one for Windows).
Later,

On Wed, 12 Apr 2006 16:30:26 -0600, Lynn Allan wrote:
Homework? What have you tried .... with what results?
Jean-Sebastien Vachon wrote:
Hi there,
I am sure what I am looking for exist somewhere but I haven't been able to find anything that works in boost::regex.
What I am looking for is a regular expression that will validate a filename according to the specification of the OS (UNIX systems and Windows).
I would be very grateful if someone could provide me with these two expressions (one for Linux and one for Windows).
Later,
Hi, So far the best expression I have for Windows is: (((((^([a-z]:\\?)|(^\\\\[^\\;:\'\"<>]+\\[^\\]+\\)))))(((([^\\;:\'\"\<\>]+)\\)*)*)((([^\\;:\'\"<>]*))(\.?([^\\:;\'\"<>]+)))?) I used Visual RegExp 3.0 to test and debug my expression(s). It is a very nice tool. The For UNIX I am currently using: ((/([^/]+)?)*)(([^/.]*).?([^/])*) I haven't tested it completely but it seems to do the job. If you spot any potential problem please let me know. Later,

((((|(^\\\\[^\\;:\'\"<>]+\\[^\\]+\\)))))(((([^\\;:\'\"\<\>]+)\\)*)*)((([^\\;:\'\"<>]*))(\.?([^\\:;\'\"<>]+)))?)
Is the (^([a-z]:\\?) part supposed to match "c:" ? If so it won't because of the literal "?". There are several possible candidates at http://regexlib.com BTW as well. John.

On Thu, 13 Apr 2006 10:59:54 +0100, John Maddock wrote:
((((|(^\\\\[^\\;:\'\"<>]+\\[^\\]+\\)))))(((([^\\;:\'\"\<\>]+)\\)*)*)((([^\\;:\'\"<>]*))(\.?([^\\:;\'\"<>]+)))?)
Is the (^([a-z]:\\?) part supposed to match "c:" ? If so it won't because of the literal "?".
There are several possible candidates at http://regexlib.com BTW as well.
John.
In the (^([a-z]:\\?) I want to make the last \ optionnal not to have a literal ?. Should I change this to: "(^([a-z]:\\\?)" ?

In the (^([a-z]:\\?) I want to make the last \ optionnal not to have a literal ?.
Should I change this to: "(^([a-z]:\\\?)" ?
The the one thing you should not do :-) If the string is embedded in your program as a string literal, then the compiler will replace each \\ with \ before regex sees it, so "\\?" matches a literal "?" and "\\\\?" is an optional backslash. Of course all your other backslashes may need doubling up as well if you intend them to be literal backslashes rather than regex-escapes :-) John.

Jean-Sebastien Vachon wrote:
On Thu, 13 Apr 2006 10:59:54 +0100, John Maddock wrote:
((((|(^\\\\[^\\;:\'\"<>]+\\[^\\]+\\)))))(((([^\\;:\'\"\<\>]+)\\)*)*)((([^\\;:\'\"<>]*))(\.?([^\\:;\'\"<>]+)))?) Is the (^([a-z]:\\?) part supposed to match "c:" ? If so it won't because of the literal "?".
There are several possible candidates at http://regexlib.com BTW as well.
John.
In the (^([a-z]:\\?) I want to make the last \ optionnal not to have a literal ?.
Should I change this to: "(^([a-z]:\\\?)" ?
One more slash ought to do it: "(^([a-z]:\\\\?)". Think of it this way: write the pattern as you would in perl. Then double every backslash. -- Eric Niebler Boost Consulting www.boost-consulting.com

On Wed, 12 Apr 2006 16:30:26 -0600, Lynn Allan wrote:
Homework? What have you tried .... with what results?
Jean-Sebastien Vachon wrote:
Hi there,
I am sure what I am looking for exist somewhere but I haven't been able to find anything that works in boost::regex.
What I am looking for is a regular expression that will validate a filename according to the specification of the OS (UNIX systems and Windows).
I would be very grateful if someone could provide me with these two expressions (one for Linux and one for Windows).
Later,
Ok my latest version for Windows is: ((((((^([a-z^[.space.]]:\\?)?)|(^\\\\[^\\/;:\'\"<>]+\\[^\\]+\\))))) (((([^\\;/:\'\"<>]+)\\)*)*)((([^\\;:/\'\"<>[.period.]]*)) ([[.period.]]([^\\:;/\'\"<>]+))?)) so far, all the tests I did were succesful in Visual REGEXP. However, I still have one case that is problematic in boost using this expression. I believe the expression should match "C:" but it does not (in boost) Can someone spot the error? I believe I am very close of succeeding. Thanks all for your valuable help (and time)
participants (4)
-
Eric Niebler
-
Jean-Sebastien Vachon
-
John Maddock
-
Lynn Allan