tags:

views:

760

answers:

3

Why won't this do anything, output is identical to input? I'm baffled!!!

string name = ";;;'']][[ zion \\\[[[]]]"  
char[] invalidChars = System.IO.Path.GetInvalidPathChars();
string invalidString = Regex.Escape(new string(invalidChars));

string valid = Regex.Replace(name, "[" + invalidString + "]", "");
+4  A: 

What do you mean by it won't do anything? I ran the following in a console application:

string name = ";;;'']][[ zion \\\\[[[]]]";
char[] invalidChars = System.IO.Path.GetInvalidPathChars();
string invalidString = Regex.Escape(new string(invalidChars));

string valid = Regex.Replace(name, "[" + invalidString + "]", "");
Console.WriteLine(valid);

By the way your syntax was wrong, you had some unescaped characters and you were missing a semi-colon.

And I got the following result.

;;;'']][[ zion \\[[[]]]

Which is the correct result. Maybe you should ask a new question about what you are trying to do because your current approach seems to indicate that you don't have a strong understanding of Regex.

Update: Are you trying to check files names? If so you probably want to use:

System.IO.Path.GetInvalidFileNameChars();

Update: Here is a list of invalid characters that comes from that method GetInvalidPathChars()

RealInvalidPathChars = new char[] { 
        '"', '<', '>', '|', '\0', '\x0001', '\x0002', '\x0003', '\x0004', '\x0005', '\x0006', '\a', '\b', '\t', '\n', '\v', 
        '\f', '\r', '\x000e', '\x000f', '\x0010', '\x0011', '\x0012', '\x0013', '\x0014', '\x0015', '\x0016', '\x0017', '\x0018', '\x0019', '\x001a', '\x001b', 
        '\x001c', '\x001d', '\x001e', '\x001f'
     };

So basically the following are invalid path characters might include ASCII/Unicode characters 1 through 31, as well as quote ("), less than (<), greater than (>), pipe (|), backspace (\b), null (\0) and tab (\t).

None of which seem to occur in your original string.

Nick Berardi
string 'valid' is identical to 'name'
zion
He's trying to do a variant of this:http://powershell.com/cs/blogs/tips/archive/2009/06/15/checking-paths-for-invalid-path-characters.aspx
TrueWill
I'm trying to get rid of all illegal chars.
zion
None of the characters you have in that string are invalid.
Nick Berardi
ok, i'm trying to clean a string of charactrs that would be invalid in a filename. tru\\e.jpg needs to be spit out as true.jpg
zion
use GetInvalidFileNameChars() then, that will fix it
Nick Berardi
+4  A: 

Get invalid file characters using,

char[] invalidChars=System.IO.Path.GetInvalidFileNameChars();
adatapost
He is trying to copy http://powershell.com/cs/blogs/tips/archive/2009/06/15/checking-paths-for-invalid-path-characters.aspx which doesn't use that.
Nick Berardi
+2  A: 

EDIT:

I think it may just be a case of imperfect test data (along with the function change that others have suggested). Try this:

string name = "tru\\e.jpg";
char[] invalidChars = System.IO.Path.GetInvalidFileNameChars();
string invalidString = Regex.Escape(new string(invalidChars));
string valid = Regex.Replace(name, "[" + invalidString + "]", "");
Console.WriteLine(valid);

I get "true.jpg" output. I would definitely suggest much more testing before using this in production! :)

TrueWill
thanks, Invalid test data. I feel this big!
zion