/ Published in: C#
                    
                                        
Remove illegal chars from a string using regular expressions
                
                            
                                Expand |
                                Embed | Plain Text
                            
                        
                        Copy this code and paste it in your HTML
/// <summary>
/// Removes "unsafe" characters from the string.
/// </summary>
/// <param name="unsafeString">String to edit</param>
/// <returns>A "safe" string</returns>
public static string GetSafeString(string unsafeString)
{
// Grab the system invalid chars
// Then remove the chars in our IgnoreInvalidChars collection
char[] systemInvalidChars = Path.GetInvalidFileNameChars();
unacceptableChars.AddRange(systemInvalidChars);
okayChars.AddRange(IgnoreInvalidChars);
// Remove the items in unaccpetableChars
// that make okayChars.Contains(char) return true
unacceptableChars.RemoveAll(okayChars.Contains);
needEscapeChars.AddRange(NeedEscapeInRegexChars);
string regexPattern = StartingRegexPattern;
// Write each char into a string
// some chars need to be further escaped
// for regex to handle them.
for (int i = 0; i < unacceptableChars.Count; i++)
{
//If the char needs to have the escape char in front of it
// add the escape char
if (needEscapeChars.Contains(unacceptableChars[i]))
{
regexPattern = regexPattern + RegexEscapeString;
}
regexPattern = regexPattern + unacceptableChars[i];
}
regexPattern = regexPattern + ClosingRegexPattern;
return Regex.Replace(unsafeString.Trim(), regexPattern, string.Empty);
}
private static readonly string StartingRegexPattern = "[";
private static readonly string ClosingRegexPattern = "]";
private static readonly string RegexEscapeString = @"\";
private static readonly char[] IgnoreInvalidChars =
{ '\n', '\r', '\a', '\b', '\t', '\0', '\v', '\f' };
private static readonly char[] NeedEscapeInRegexChars =
{ '\\', '/' };
Comments
 Subscribe to comments
                    Subscribe to comments
                
                