|
|
|
|
![]() ![]() |
Feb 20 2008, 03:44 AM
Post
#1
|
|
|
Living at the Datacenter Group: [HOSTED] Posts: 696 Joined: 30-June 06 From: Australia Member No.: 14,219 |
Hi All,
I am using Regular Expressions in VB.NET to find illegal filename characters in a string. The user enters and name, and it then saves the file with that name. I just need to make sure that the filename doesn't contain illegal filename characters. The characters are / \ : ? " < > | (is there any others?) I am using the following regex at the moment, but it doesn't seem to be picking up backslashes. CODE Dim myRegex As New System.Text.RegularExpressions.Regex("[\/:?'=<>|]") Every other character it picks up, why just this one? Also the " character is illegal, how can i put that into the regex (at the moment it thinks its part of the code and not the regex) If there is another way to do the same thing, or a different regex, that would be nice! Thanks, -jimmy |
|
|
|
Feb 20 2008, 07:25 AM
Post
#2
|
|
|
Premium Member Group: [HOSTED] Posts: 286 Joined: 17-June 07 Member No.: 22,702 |
Have you tried escaping the \ with another \ - therefore making a \\ out of it?
The same trick might work by putting a \" instead of the " The backslash is a good guess for what the escape character is in many environments, it might be that in VB.NET it's the same as well. This post has been edited by dserban: Feb 20 2008, 07:27 AM |
|
|
|
Feb 20 2008, 09:49 AM
Post
#3
|
|
|
Premium Member Group: [HOSTED] Posts: 479 Joined: 5-November 06 Member No.: 17,016 |
For " you need to escape with " itself.
CODE "" |
|
|
|
Feb 20 2008, 07:10 PM
Post
#4
|
|
|
Way Out Of Control - You need a life :) Group: [MODERATOR] Posts: 1,980 Joined: 16-August 05 Member No.: 7,896 |
And of course you probably need to protect \ from itself (usually \ means "ignore what follows me"). So, instead of \ you should write down \\.
I have this problem with the character $ in my script, I have to protect it, so instead of $ I write down \$. So, amongs things not to be used in a filename, I think that you should add the following ones : . (the dot, ".") + (because on Unix systems a+b will try to calculate the sum of a and b ) - (same reason, substracting) =(a=b will be evaluated as "does the variable a be equal to the variable b" or a syntax error) * (the star, means "everything" when interpreted by Unix or Linux) space (the blank, " ") because "my file" will try to write down a file namde "my" and a file named "my" $ (I guess, at least on IBM mainframe, but maybe your programs are not for mainframe ?) And I'm sure that other forumers here will tell you a lot of other forbidden characters. By the way, may I suggest you, if a blank or a minus is in the filename, replace it by _ (underscore) ? I see that other progs like 4image do it that way. |
|
|
|
Feb 21 2008, 03:24 AM
Post
#5
|
|
|
Living at the Datacenter Group: [HOSTED] Posts: 696 Joined: 30-June 06 From: Australia Member No.: 14,219 |
Thanks guys, those changes worked a treat! (I've never liked Regular Expressions, or maybe they haven't liked me) Yordan, thanks for the little extras - I've added them in also, you can never to too safe when it comes to end users and their inputs
Cheers, -jimmy |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 8th September 2008 - 06:31 AM |