Click here to Skip to main content
15,884,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi,

i cant find a regex pattern for unallowed signs ( < > ? " : | \ / * ) in a dataname, could anybody give me this pattern please?

thank you
Posted

1 solution

Try this:
C#
string regexSearch = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
Regex r = new Regex(string.Format("[{0}]", Regex.Escape(regexSearch)));

Cheers,
Edo
 
Share this answer
 
Comments
niko_tells 21-Feb-13 2:05am    
Thank you very much!
But i hardly used Regex until now, how do i use it with my string now?
I tried r.Machtes(MyString);
But it seems not legit
Joezer BH 21-Feb-13 2:08am    
String illegal = "Your string here. The regex will remove the illegal chars from it.";
... The code in the sample I showed ...
illegal = r.Replace(illegal, "");
niko_tells 21-Feb-13 2:14am    
Thank you very much!
niko_tells 21-Feb-13 2:18am    
Ok i got it, it works like that:

string nameWithoutUnallowedSigns = RemoveSigns.Replace(name, "");
Joezer BH 21-Feb-13 2:29am    
instead of your:
string tempName = name;
RemoveSigns.Replace(name, "");

use:
string tempName = RemoveSigns.Replace(name, "");

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900