Validating file names using regex

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Validating file names using regex

Jean-Sebastien Vachon
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,

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Validating file names using regex

Nat Goodspeed
> -----Original Message-----
> From: [hidden email] [mailto:boost-users-
> [hidden email]] On Behalf Of Jean-Sebastien Vachon
> Sent: Wednesday, April 12, 2006 3:38 PM
> To: [hidden email]
> Subject: [Boost-users] Validating file names using regex
>
> 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).

[Nat] This isn't what you're asking for -- but would it work to
instantiate a boost::filesystem::path with the putative pathname in
question and an appropriate checker?
_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Validating file names using regex

Jean-Sebastien Vachon
On Wed, 12 Apr 2006 16:31:30 -0400, Nat Goodspeed wrote:

>> -----Original Message-----
>> From: [hidden email] [mailto:boost-users-
>> [hidden email]] On Behalf Of Jean-Sebastien Vachon
>> Sent: Wednesday, April 12, 2006 3:38 PM
>> To: [hidden email]
>> Subject: [Boost-users] Validating file names using regex
>>
>> 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).
>
> [Nat] This isn't what you're asking for -- but would it work to
> instantiate a boost::filesystem::path with the putative pathname in
> question and an appropriate checker?

Hi,

I am currently investigating this solution.

Thanks for the suggestion anyway...

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Validating file names using regex

l_d_allan
In reply to this post by Jean-Sebastien Vachon
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,

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Validating file names using regex

Jean-Sebastien Vachon
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,

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Validating file names using regex

Jean-Sebastien Vachon
In reply to this post by l_d_allan
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)


_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Validating file names using regex

John Maddock
In reply to this post by Jean-Sebastien Vachon
> ((((|(^\\\\[^\\;:\'\"<>]+\\[^\\]+\\)))))(((([^\\;:\'\"\<\>]+)\\)*)*)((([^\\;:\'\"<>]*))(\.?([^\\:;\'\"<>]+)))?)

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.

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Validating file names using regex

Jean-Sebastien Vachon
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]:\\\?)" ?

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Validating file names using regex

John Maddock
> 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.

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Validating file names using regex

Eric Niebler
In reply to this post by Jean-Sebastien Vachon
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
_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users